Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ delete
Hello,
 
I am working on a program which has a function(GetMyRecordsfrmDB()) which fetches data from Database and stores the records in a LIST object (User Defined - class MY_LIST). A pointer to this LIST object is then sent back as Return type. The LIST is created using 'new' but as we are sending pointer to this LIST as return type, its not getting 'delete'd in the function which (i guess) is responsible for Memory Leak in my program.
 
Note : The Code is from a program which uses CORBA Architechture (Client-Server model). This function is executed at Server Side and the function call is made from Client side.
 
MY_LIST* MyClass :: GetMyRecordsfrmDB ( someArgs1&, someArgs2&)
{
MY_LIST *pList = NULL;
 
pList = new MY_LIST;  
 
fillList (.., pList, ..); //some code which fetches data and store in pList;

return (pList);   // no 'delete' for *pList in whole function
}
 
Please tell me am i right to guess that there is memory leak in this funtion due to above mentioned reason, And if yes, then how can i rectify it ?
 
Any help would be highly appreciated.
Thanks in Advance
Posted 28-Mar-11 1:21am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You may ask the client to provide a pointer to valid (client allocated) MY_LIST object.
  Permalink  
v2
Comments
Olivier Levrey at 28-Mar-11 11:24am
   
My 5. I agree with Cpallini. Let the client allocate the list and pass it to the function. Then the function can fill the list. The client will then be responsible for deleting the list.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The delete doesn't necessarily have to be in the same function that called new, but it must take place either when you're done using that pList or upon the termination of the executable (whatever case is applicable).
 
If they are different apps, then you should probably manage the memory in a smarter way, as cpallini stated, either pass a pointer that the client will be able to delete or have some sort of memory management on your server (i.e. delete allocated memory based on some event).
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 28 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100