Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check for memory leaks

12 views
Skip to first unread message

thor...@newsgroup.nospam

unread,
Oct 31, 2008, 2:22:58 PM10/31/08
to
Hi,

I'm using the static code analysis switch /analysis of the cl compiler, but
this does not detect memory leaks. Are there any tools that can do so at run
time or compile time?

Thanks, Thorsten

Jialiang Ge [MSFT]

unread,
Nov 3, 2008, 12:03:52 AM11/3/08
to
Good morning Thorsten. Welcome to Microsoft Newsgroup Support service! My
name is Jialiang Ge [MSFT]. It's my pleasure to work with you on this
thread.

I understand your concerns that the /analyze switch of the cl compiler does
not output some memory leaks that you expect to detect. PREfast is the
codename for the /analyze compiler switch and SAL annotations:
http://blogs.msdn.com/vcblog/archive/2008/02/05/prefast-and-sal-annotations.aspx
http://download.microsoft.com/download/a/f/7/af7777e5-7dcd-4800-8a0a-b18336565f5b/PREfast_steps_80.doc
According to its documentation, PREfast is actually capable of detecting the
memory leaks to some extent. For example:

char *x;
x = (char *)malloc(10);
if (x) {
x = (char *)realloc(x, 512);
}

This piece of code generates a compiler warning from the /analyze switch and
reports the potential memory leak:

warning C6308: 'realloc' might return null pointer: assigning null pointer
to 'x', which is passed as an argument to 'realloc', will cause the original
memory block to be leaked c:\code\testleak.cpp 34 TestLeak

However, some memory leak scenarios cannot be detected by /analyze. For
example:

void f(void)
{
void* s;
s = malloc(5000); /* get memory */
return; /* memory leak */
}
int _tmain(int argc, _TCHAR* argv[])
{
f();
}

As the article
http://blogs.msdn.com/vcblog/archive/2008/02/05/prefast-and-sal-annotations.aspx
says,
"While there is no tool that can do everything, and some overlap in their
purpose, used in combination they ensure high quality product
development.",
in the following content, I will introduce several other tools for
memory-leak detection. We may combine them and get a more accurate result.

1. Memory Leak detection during debugging:
http://msdn.microsoft.com/en-us/library/e5ewb1h3(VS.80).aspx

For example (this is the memory leak scenario that /analyze cannot detect),
void f(void)
{
void* s;
s = malloc(5000); /* get memory */
return; /* memory leak */
}
int _tmain(int argc, _TCHAR* argv[])
{
f();
_CrtDumpMemoryLeaks();
}

_CrtDumpMemoryLeaks dumps these info in the Output dialog of Visual Studio:

Detected memory leaks!
Dumping objects ->
c:\code\testleak.cpp(16) : {106} normal block at 0x00333FF0, 5000 bytes
long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
The program '[8128] TestLeak.exe: Native' has exited with code 0 (0x0).

3. Umdhtools.exe: How to use Umdh.exe to find memory leaks
(runtime)
http://support.microsoft.com/?kbid=268343

4. How to use Memory Pool Monitor (Poolmon.exe) to troubleshoot kernel mode
memory leaks
(runtime)
http://support.microsoft.com/?kbid=177415

5. Using Performance Monitor to Identify a Pool Leak
(runtime)
http://support.microsoft.com/?kbid=130926

6. Windows Leaks Detector
http://sourceforge.net/projects/winleak
(runtime)
This is an open source software in sf.net for Windows application memory
leak detection.

7. There are many other 3rd party tools that can detect memory leaks:
http://en.wikipedia.org/wiki/Memory_leak
See the section "Programming issues"

Please let me know whether the above info is helpful or not. If you have any
other questions or concerns, feel free to tell me.

Have a very nice day!

Regards,
Jialiang Ge (jia...@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msd...@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within 2
business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

<thor...@newsgroup.nospam> wrote in message
news:04C26A6E-F7B3-4996...@microsoft.com...

thor...@newsgroup.nospam

unread,
Nov 4, 2008, 1:34:59 PM11/4/08
to
Thank you very much for this elaborate overview. I think it answers all my
questions for the moment.

Jialiang Ge [MSFT]

unread,
Nov 5, 2008, 2:09:27 AM11/5/08
to
You are welcome Thorsten!
Glad to help

Regards,
Jialiang Ge (jia...@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msd...@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================

0 new messages