Memory leak when return char* from DLL
-
Hi , I have a problem of memory leak when we call a function from win 32 DLL in our MFC program. DLL is C type and return value of my DLL is Char* and it is locally defined in function of DLL. and size is 1024 byte. when functon call four times then MFC application hold 4 KB Extra memory.
-
Hi , I have a problem of memory leak when we call a function from win 32 DLL in our MFC program. DLL is C type and return value of my DLL is Char* and it is locally defined in function of DLL. and size is 1024 byte. when functon call four times then MFC application hold 4 KB Extra memory.
Is the DLL allocating memory that the
char*
points to, on the heap? Probably then the calling application must free this memory up? Do you have any docs for that DLL? Or if you have the source code, you may verify this.It is a crappy thing, but it's life -^ Carlo Pallini
-
Hi , I have a problem of memory leak when we call a function from win 32 DLL in our MFC program. DLL is C type and return value of my DLL is Char* and it is locally defined in function of DLL. and size is 1024 byte. when functon call four times then MFC application hold 4 KB Extra memory.
-
When returning char* as return value, you must allocate memory in heap(using malloc or new), from your DLL and deallocate this memory(free or delete) after usage from the calling applciation.
prvn
i m using New and Delete in calling application(MFC). but "delete []cData;" line have error runtime. if we use cData='\0' and after this "delete []cData;" then memory leak still continue.
-
Is the DLL allocating memory that the
char*
points to, on the heap? Probably then the calling application must free this memory up? Do you have any docs for that DLL? Or if you have the source code, you may verify this.It is a crappy thing, but it's life -^ Carlo Pallini
i m using New and Delete in calling application(MFC). but "delete []cData;" line have error runtime. if we use cData='\0' and after this "delete []cData;" then memory leak still continue. how can free memory in MFC application.
-
i m using New and Delete in calling application(MFC). but "delete []cData;" line have error runtime. if we use cData='\0' and after this "delete []cData;" then memory leak still continue. how can free memory in MFC application.
If the memory is allocated with
new
in the DLL, then clean it up withdelete
after you've used the data. Where is the difficulty? I don't see anything more to 'discuss' about here.It is a crappy thing, but it's life -^ Carlo Pallini
-
i m using New and Delete in calling application(MFC). but "delete []cData;" line have error runtime. if we use cData='\0' and after this "delete []cData;" then memory leak still continue.
shivanandgupta wrote:
if we use cData='\0' and after this "delete []cData;" then memory leak still continue.
That should not be a surprise... Either you are just making a silly mistake (which we all do), or it's a bit more complex and you are allocating and freeing from different heaps. This could happen if you statically link to the C++ runtime. One simple choice would be to use IMalloc to allocate / free chunks of memory. Look at CoGetMalloc etc for documentation. I still think it's likely to be something dumb... Are you sure the memory is not already freed elsewhere? Iain.
In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!
-
If the memory is allocated with
new
in the DLL, then clean it up withdelete
after you've used the data. Where is the difficulty? I don't see anything more to 'discuss' about here.It is a crappy thing, but it's life -^ Carlo Pallini
Actually i am using static linking a dll. so when we used delete then runtime error occor about heap
-
shivanandgupta wrote:
if we use cData='\0' and after this "delete []cData;" then memory leak still continue.
That should not be a surprise... Either you are just making a silly mistake (which we all do), or it's a bit more complex and you are allocating and freeing from different heaps. This could happen if you statically link to the C++ runtime. One simple choice would be to use IMalloc to allocate / free chunks of memory. Look at CoGetMalloc etc for documentation. I still think it's likely to be something dumb... Are you sure the memory is not already freed elsewhere? Iain.
In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!
Thanks for Reply i am calling DLL statically
-
i m using New and Delete in calling application(MFC). but "delete []cData;" line have error runtime. if we use cData='\0' and after this "delete []cData;" then memory leak still continue.
i don't have any idea to use Imalloc and cogetmalloc function to free memory. i m using statically DLL not a COM
-
Hi , I have a problem of memory leak when we call a function from win 32 DLL in our MFC program. DLL is C type and return value of my DLL is Char* and it is locally defined in function of DLL. and size is 1024 byte. when functon call four times then MFC application hold 4 KB Extra memory.
If the DLL allocates the memory using
new
, it should also be the one todelete[]
it. So, let the DLL export a new function to free memory. In your MFC program, you'd call the original method to allocate the char*, and afterwards you'd pass this pointer to the freeing function of the DLL, which would justdelete[]
it.
Too many passwords to remember? Try KeePass Password Safe!
-
If the DLL allocates the memory using
new
, it should also be the one todelete[]
it. So, let the DLL export a new function to free memory. In your MFC program, you'd call the original method to allocate the char*, and afterwards you'd pass this pointer to the freeing function of the DLL, which would justdelete[]
it.
Too many passwords to remember? Try KeePass Password Safe!
If the application is being build with the runtime libary (CRT) as a DLL (Recommended by Microsoft), then it should be possible to delete memory in the application that have been allocated by the DLL.
-
If the application is being build with the runtime libary (CRT) as a DLL (Recommended by Microsoft), then it should be possible to delete memory in the application that have been allocated by the DLL.
Correct. Based on the original question and replies, it was my impression that different heaps/CRTs are being used (see static linkage post), that's why I suggested this way. Of course, using only one CRT will solve the problem, too.
Too many passwords to remember? Try KeePass Password Safe!
-
Correct. Based on the original question and replies, it was my impression that different heaps/CRTs are being used (see static linkage post), that's why I suggested this way. Of course, using only one CRT will solve the problem, too.
Too many passwords to remember? Try KeePass Password Safe!
I m unable to use CRT can u give some kind of code
-
Correct. Based on the original question and replies, it was my impression that different heaps/CRTs are being used (see static linkage post), that's why I suggested this way. Of course, using only one CRT will solve the problem, too.
Too many passwords to remember? Try KeePass Password Safe!
Thank you for confirming this. I'm in a situation myself where I'm considering to convert static-libraries into DLL's to improve linker times. The entire application is already using runtime library as DLL, so the conversion should be a matter of exporting all needed classes in the static libraries.
-
I m unable to use CRT can u give some kind of code
Just follow what Dominik Reichl said in the first comment about ensuring that memory allocated in the DLL is released by the DLL again. Return the char-pointer to the DLL and let the DLL release it again.
-
Just follow what Dominik Reichl said in the first comment about ensuring that memory allocated in the DLL is released by the DLL again. Return the char-pointer to the DLL and let the DLL release it again.
return data char* is locally defined in DLL. so how can release memory of that variable after returning char* variable
-
return data char* is locally defined in DLL. so how can release memory of that variable after returning char* variable
When returning the char* then you take a copy of the char*, but they both point to the same memory in the heap of the DLL. Just pass the copy of the char* from the application to the DLL again, and let the DLL release the memory in its heap. You might consider returning a CString instead of a char*, and ensure that when the CString moves from DLL boundary to the Application boundary that a copy is made of the CString.
-
i m using New and Delete in calling application(MFC). but "delete []cData;" line have error runtime. if we use cData='\0' and after this "delete []cData;" then memory leak still continue.
shivanandgupta wrote:
if we use cData='\0' and after this "delete []cData;" then memory leak still continue.
Well, if you set cData to '\0' you are manipulating the pointer, not the data it contains. cData is set to null, and that is what you try to delete (and it always succeeds.) Doesn't the DLL supply an API for freeing this string? IMHO dll's should always be designed that way.
-
When returning the char* then you take a copy of the char*, but they both point to the same memory in the heap of the DLL. Just pass the copy of the char* from the application to the DLL again, and let the DLL release the memory in its heap. You might consider returning a CString instead of a char*, and ensure that when the CString moves from DLL boundary to the Application boundary that a copy is made of the CString.
I have try but it is not working. return char* is defined in dll's function as a local variable. it will work well when we defined static variable. but i don't want to create static variable in my DLL.