Memory leaks in the C runtime library

Posted on 2001-07-16
Medium Priority
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 TomP@Rebellion.co.uk.
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt


Accepted Solution

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

Thank you
E-E Moderator

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

839 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