Solved

Tracking down memory leaks in StrCore.cpp by using _CrtSetBreakAlloc

Posted on 2007-03-23
8
3,127 Views
Last Modified: 2011-10-03
I’m trying to track down some memory leaks in StrCore.cpp by using _CrtSetBreakAlloc in my watch window and I can’t get it to work.

The memory leak looks like this and there are hundreds of them:
f:\sp\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {8164} normal block at 0x196336A8, 272 bytes long.
 Data: <, ?x    ÿ       > 2C 08 3F 78 00 00 00 00 FF 00 00 00 01 00 00 00

Maybe I’m not typing it into my Watch window correctly, I tried alot of different ways.  Here is exactly what I have been trying.

Thes give “CXX0017: Error: symbol “_CrtSetBreakAlloc” not found”:
_CrtSetBreakAlloc
__p__crtBreakAlloc()
_CrtSetBreakAlloc(8164)
_CrtSetBreakAlloc(8164);
{,,msvcrtd.dll}_crtBreakAlloc
_CrtSetBreakAlloc(8164)

These give “CXX0036: Error: bad context {…} specification”:
{msvcrtd.dll}*__p__crtBreakAlloc(8164)
{,,msvcrtd.dll}*__p__crtBreakAlloc(8164)
{,,msvcrtd.dll}_crtBreakAlloc(8164)
{,,msvcrtd.dll}_crtBreakAlloc(8164)

Any ideas on how I can fix this or how I can track down these memory leaks?
0
Comment
Question by:bigsteve87
  • 4
  • 3
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 18782309
You can only use '_CrtSetBreakAlloc()' in debug builds when you add it to your code like in

#include <malloc.h>
#include <crtdbg.h>

void main( )
{
        long allocReqNum;
        char *my_pointer;

        /*
         * Allocate "my_pointer" for the first
         * time and ensure that it gets allocated correctly
         */
        my_pointer = malloc(10);
        _CrtIsMemoryBlock(my_pointer, 10, &allocReqNum, NULL, NULL);

        /*
         * Set a breakpoint on the allocation request
         * number for "my_pointer"
         */
        _CrtSetBreakAlloc(allocReqNum+2);
        _crtBreakAlloc = allocReqNum+2;

        /*
         * Alternate freeing and reallocating "my_pointer"
         * to verify that the debugger halts program execution
         * when it reaches the allocation request
         */
        free(my_pointer);
        my_pointer = malloc(10);
        free(my_pointer);
        my_pointer = malloc(10);
        free(my_pointer);
}

Typing it in your debugger's watch window won't help you here.

0
 
LVL 86

Expert Comment

by:jkr
ID: 18782328
BTW - an easier way to find your leaks:

Add the following to a central header file:

#ifdef _DEBUG
#ifndef _DBG_NEW

#include <crtdbg.h>

inline void* __operator_new(size_t __n) {
     return ::operator new(__n,_NORMAL_BLOCK,__FILE__,__LINE__);
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
     return ::operator new(__n,_NORMAL_BLOCK,__fname,__line);
}
inline void _cdecl operator delete(void* __p,const char*,int) {
     ::operator delete(__p);
}

#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW


#endif // _DBG_NEW
#else

#define __operator_new(__n) operator new(__n)

#endif

Then, add

               int tmpFlag;

               // Get the current state of the flag
               // and store it in a temporary variable
               tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

               // Turn On (OR) - Keep freed memory blocks in the
               // heap’s linked list and mark them as freed
               tmpFlag |= _CRTDBG_LEAK_CHECK_DF;

               // Set the new state for the flag
               _CrtSetDbgFlag( tmpFlag );


to your app's start code and call

               _CrtDumpMemoryLeaks ();


to before the program ends and you'll get the line number where the allocation occured.

(Ref.: http:Q_21009673.html)
0
 

Author Comment

by:bigsteve87
ID: 18782774
I tried the easier way where you define _DBG_NEW but it came up with alot of errors.
 I'll try the _CrtIsMemoryBlock solution now.  

Here are the errors it came up with:
3>C:\Program Files\Microsoft Visual Studio 8\VC\include\xdebug(32) : error C2365: 'operator new' : redefinition; previous definition was 'function'
3>C:\Program Files\Microsoft Visual Studio 8\VC\include\xdebug(32) : error C2491: 'new' : definition of dllimport data not allowed
0
 
LVL 86

Expert Comment

by:jkr
ID: 18782818
Hm, just checked with a project that uses this (also VC8), that should work like:

#ifdef _DEBUG
#ifndef _DBG_NEW

#include <crtdbg.h>

inline void* __operator_new(size_t __n) {
      return ::operator new(__n,_NORMAL_BLOCK,__FILE__,__LINE__);
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
      return ::operator new(__n,_NORMAL_BLOCK,__fname,__line);
}
inline void _cdecl operator delete(void* __p,const char*,int) {
      ::operator delete(__p);
}

#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW

#endif // _DBG_NEW
#else

#define __operator_new(__n) operator new(__n)

#endif // _DEBUG
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:bigsteve87
ID: 18782941
Sry, I changed it and am still getting the same errors.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18783121
Add this below your header file:

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

Best Regards,
DeepuAbrahamK
0
 
LVL 86

Expert Comment

by:jkr
ID: 18783363
Actually, in order for the above to work, you need to locate that section and comment it out:

/*
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
*/

since that is mostlikely to cause the compile error.
0
 

Author Comment

by:bigsteve87
ID: 18783447
Thanks guys, I was able to track it down using  _crtBreakAlloc.
I tried using the other method with the modifications with no luck.  Could have to do with my project being a mess because I'm in the process of converting from VC98 to VS2005.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now