Click here to Skip to main content
13,043,491 members (51,738 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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 0:21am
Rate this: bad
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.
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.
Rate this: bad
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).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 28 Mar 2011
Copyright © CodeProject, 1999-2017
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