Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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

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).
 
Share this answer
 
You may ask the client to provide a pointer to valid (client allocated) MY_LIST object.
 
Share this answer
 
v2
Comments
Olivier Levrey 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900