Web lists-archives.com

Re: [Mingw-users] CRT lib: potential memory leakage




Eli,

On 05-May-17 15:15, Eli Zaretskii wrote:
>> From: "Jannick" <thirdedition@xxxxxxx>
>> Date: Fri, 5 May 2017 14:33:02 +0200
>>
>> it appears that the CRT lib produces some memory leakage: The small program compiled with mingw32-gcc
>> (5.3.0)
>>
>> #include <stdlib.h>
>>
>> int main (int argc, char** argv)
>>
>> {
>>
>> free(argv[0]);
>>
>> return 0;
>>
>> }
>>
>> runs through like a charm which is suggesting that the CRT lib’s terminating clean-up work misses the exe
>> path (argv[0]) somehow.
> First, what is the purpose of freeing memory just before the program
> exits?  That memory will be freed by the OS anyway.

Disagree. It is my sad experience that Windows in particular does NOT 
free all memory allocated in an exiting binary - it probably should, but 
does NOT. We learned it the hard way, getting gigs of RAM "reserved" 
(but not freed), until we found a few arrays we didn't free just before 
leaving, and the thing was accumulating over multiple runs of the same 
binary.

A good part of our job is actually making sure that absolutely 
everything we allocate is freed, down to the last byte. It is ESSENTIAL 
(under Win at least). And we are running the very latest version of Win10.

> And second, the argv array is not allocated by the MinGW runtime, it
> is allocated by MSVCRT.DLL, the Windows CRT shared library.  Since we
> have no idea how it is allocated, calling 'free' on it in your program
> is a bad idea, because you cannot be sure it was allocated by a call
> to the same 'malloc' version that corresponds to the 'free' you want
> to use.
>
> IOW: don't do that!

Agreed!

Best,

Emanuel

>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> MinGW-users mailing list
> MinGW-users@xxxxxxxxxxxxxxxxxxxxx
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.
>
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
> Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

This list observes the Etiquette found at 
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe