Memory leaks in the C runtime library

Posted on 2001-07-16
Last Modified: 2008-02-01
I've tried to compile with VC++ under Unicode, defining UNICODE and created a simple wWinMain() function, and defined my entry point correctly. The program compiles and runs, but I get memory leaks reported by NuMega BoundsChecker from the two standard files in the C runtime library - stdargv.c and stdanvp.c, even if all I do is just return from the WinMain (i.e. there is no code in WinMain. ) The memory leaks are quite bad, totalling about 6K. Any ideas why this is?
Please post your replies to
Question by:Tom_P
LVL 22

Expert Comment

ID: 6285758
Are you using MFC then?

Did you make wWinMainCRTStartup() the entrypoint procedure in the project settings (link tab, ouput category)?

Author Comment

ID: 6285874
No, I am using straight Windows. And yes, I did specify the entry point correctly (in fact i think that the program wouldn't link otherwise. )
Any ideas would be very appreciated.

Expert Comment

ID: 6288447
Does the problem occur in the release build or only the debug build?  The debug C++ runtime uses a different set of memory functions, and these may contain leaks.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 6289172
I worked it out last night.
The problem is the following:
If (and only if) building a non-MFC application for Windows do the following:

1) #define UNICODE //not _UNICODE
2) specify WinMain as your main function (not wWinMain)
3) leave the compiler entry point set to normal (not       wWinMainCRTSStartup)

On my machine with VC++ 6.0 SP4 this builds a unicode program (i.e. all functions called are the <fnname>W's as
opposed to <fnname>A's, e.g. MessageBoxW gets called correctly, not MessageBoxA )

Thanks for your help.
P.S. Is there any way I could give the points to myself?
LVL 22

Expert Comment

ID: 6289259
>> give the points to myself?
No, but you can delete the question and they will be refunded.

What you've done is create a progrma that calls unicode functions, but has an ASCII entry point.  Is that what you wanted?

Author Comment

ID: 6294466
Well not quite neitod, but it doesn't leak memory. Can you think of any reason why having an ascii entry point would cause trouble?
I am really getting tired of this. Have a look at the following piece of code:
int APIENTRY WinMain( /*params here*/)
 return 0;

Author Comment

ID: 6294497
When comiled under ANSI, no problems.
If I define _UNICODE, the versions of all the fns. called will actually be the char * versions. Which leads me to believe that _UNICODE is MFC specific.

When I define UNICODE, all fns. are wide character ones.
If I change WinMain to wWinMain and Entry point to wWinMainCRTStartup, compiles and links fine, BUT! there is a 6k memory leak in the runtime library.
When I use WinMain and normal entry point, no problems.
This is really annoying me now.
Anyway, I'd be gratefull for any ideas on this from anyone.
I use MSCV++ 6.0 with SP4, and the tool that reports memory leaks is BoundsChecker version 6.5.
Thanks, Tom
LVL 22

Expert Comment

ID: 6294678
>> Can you think of any reason why having an ascii entry
>> point would cause trouble?
No, but I'm only vaguely aquanted with the VC start-up code.  I've stepped through it a few times and looked at parts in detail, but I'm not thrououghly familiar with it.

>> _UNICODE is MFC specific.
yes, moistly it controls what libraries an MFC program links to and then in AFXv_w32.h it defines the vlaue of UNICODE which is the value used by windows.h.

You might try stepping through the startup-code and see where it gets lost.  Most likely it is something to do with translating the parameters to wide haracter

I wouldn't loose too much sleep over this.  (Well, actually I probably would, but I'm too compulsive too.)  This is not goin to be a steady memory leak.  i.e. it won't continue to consume memory during the program life and eventually cause a crash.  In fact, it you get it fixed, it probably won't change anything.  Most likely the memory will not be deleted until the program returns from the entry point anyways.  So it getting it delted at the program end, rather than never deleted, is not really of much value.   But I hate loose ends myself....

Author Comment

ID: 6297619
That is what I am thinking, I know (I hope) as soon as the code finishes the memory leak will be cleaned up by windows, but - I like to know what's going on. On the other hand, I have been assuming all the way that BoundChecker got it right... maybe there is a problem elsewhere..?
Anyway, thanks for all your input.
LVL 22

Expert Comment

ID: 6297752
>> I know (I hope) as soon as the code finishes the memory leak will be cleaned
When the program ends, the memory is returned to the OS.  There is no doubt about that.  
LVL 11

Expert Comment

ID: 6828308
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to refund the points and PAQ at zero points since you found your own answer.

The link to the Community Support area is:


Accepted Solution

Computer101 earned 0 total points
ID: 6845969
Points reduced to 0 and placed in PAQ

Thank you
E-E Moderator

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question