sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Harsha's Avatar

How to find Memory Leak


Question posted by: Harsha (Guest) on July 19th, 2005 09:13 PM
I have been working on VB, ASP for quite a long time. Very Recently (
From past 1 month) I am managing a VC++ project. I am trying to use
the code which i downloaded from the internet to find the memory leak
in the VC++ dll which is causing the servers to crash. I am getting
the compilation error on this particular line

void * __cdecl operator new(unsigned int size,const char *file, int
line)

error C2059: syntax error : 'string'
error C2091: function returns function
error C2809: 'operator new' has no formal parameters

which I don't know how to resolve. It would be a great help if any
body throw some light on this . Thank in advance

Below is the code which i cut and pasted from your article.

Header File
---------------
/ stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__ADEF40E8_7DC0_4B4B_9B55_DC8 B3C32A60E__INCLUDED_)
#define AFX_STDAFX_H__ADEF40E8_7DC0_4B4B_9B55_DC8B3C32A60E __INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#include <list>
#include <iostream>
using namespace std ;
int size;
const char *file;
int line;
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common
Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif
#define new DEBUG_NEW

CPP File
-----------
// stdafx.cpp : source file that includes just the standard includes
// Sample.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#define __cdecl
#include "stdafx.h"
typedef struct {
DWORD address;
DWORD size;
char file[64];
DWORD line;
} ALLOC_INFO;
typedef list<ALLOC_INFO*> AllocList;
AllocList *allocList;
void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum)
{
ALLOC_INFO *info;
if(!allocList) {
allocList = new(AllocList);
}
info = new(ALLOC_INFO);
info->address = addr;
strncpy(info->file, fname, 63);
info->line = lnum;
info->size = asize;
allocList->insert(allocList->begin(), info);
};
void RemoveTrack(DWORD addr)
{
AllocList::iterator i;
if(!allocList)
return;
for(i = allocList->begin(); i != allocList->end(); i++)
{
if((*i)->address == addr)
{
allocList->remove((*i));
break;
}
}
};
void DumpUnfreed()
{
AllocList::iterator i;
DWORD totalSize = 0;
char buf[1024];
if(!allocList)
return;
for(i = allocList->begin(); i != allocList->end(); i++) {
sprintf(buf, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d unfreed\n",
(*i)->file, (*i)->line, (*i)->address, (*i)->size);
OutputDebugString(buf);
totalSize += (*i)->size;
}
sprintf(buf, "-----------------------------------------------------------\n");
OutputDebugString(buf);
sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
OutputDebugString(buf);
};
#ifdef _DEBUG
void * __cdecl operator new(unsigned int size,const char *file, int
line)
{
void *ptr = (void *)malloc(size);
AddTrack((DWORD)ptr, size, file, line);
return(ptr);
};
inline void __cdecl operator delete(void *p)
{
RemoveTrack((DWORD)p);
free(p);
};
#endif
1 Answer Posted
Amol Chavan's Avatar
Guest - n/a Posts
#2: Re: How to find Memory Leak

If you are using the MFC part of it then you can use
CMemoryState object to find memory leaks ....

Amol

Join Bytes! (Harsha) wrote in message news:<caa597ca.0311121401.2867567d@posting.google.com>...[color=blue]
> I have been working on VB, ASP for quite a long time. Very Recently (
> From past 1 month) I am managing a VC++ project. I am trying to use
> the code which i downloaded from the internet to find the memory leak
> in the VC++ dll which is causing the servers to crash. I am getting
> the compilation error on this particular line
>
> void * __cdecl operator new(unsigned int size,const char *file, int
> line)
>
> error C2059: syntax error : 'string'
> error C2091: function returns function
> error C2809: 'operator new' has no formal parameters
>
> which I don't know how to resolve. It would be a great help if any
> body throw some light on this . Thank in advance
>
> Below is the code which i cut and pasted from your article.
>
> Header File
> ---------------
> / stdafx.h : include file for standard system include files,
> // or project specific include files that are used frequently, but
> // are changed infrequently
> //
> #if !defined(AFX_STDAFX_H__ADEF40E8_7DC0_4B4B_9B55_DC8 B3C32A60E__INCLUDED_)
> #define AFX_STDAFX_H__ADEF40E8_7DC0_4B4B_9B55_DC8B3C32A60E __INCLUDED_
> #if _MSC_VER > 1000
> #pragma once
> #endif // _MSC_VER > 1000
> #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
> #include <afxwin.h> // MFC core and standard components
> #include <afxext.h> // MFC extensions
> #include <afxdisp.h> // MFC Automation classes
> #include <list>
> #include <iostream>
> using namespace std ;
> int size;
> const char *file;
> int line;
> #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common
> Controls
> #ifndef _AFX_NO_AFXCMN_SUPPORT
> #include <afxcmn.h> // MFC support for Windows Common Controls
> #endif // _AFX_NO_AFXCMN_SUPPORT
> #ifdef _DEBUG
> #define DEBUG_NEW new(__FILE__, __LINE__)
> #else
> #define DEBUG_NEW new
> #endif
> #define new DEBUG_NEW
>
> CPP File
> -----------
> // stdafx.cpp : source file that includes just the standard includes
> // Sample.pch will be the pre-compiled header
> // stdafx.obj will contain the pre-compiled type information
> #define __cdecl
> #include "stdafx.h"
> typedef struct {
> DWORD address;
> DWORD size;
> char file[64];
> DWORD line;
> } ALLOC_INFO;
> typedef list<ALLOC_INFO*> AllocList;
> AllocList *allocList;
> void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum)
> {
> ALLOC_INFO *info;
> if(!allocList) {
> allocList = new(AllocList);
> }
> info = new(ALLOC_INFO);
> info->address = addr;
> strncpy(info->file, fname, 63);
> info->line = lnum;
> info->size = asize;
> allocList->insert(allocList->begin(), info);
> };
> void RemoveTrack(DWORD addr)
> {
> AllocList::iterator i;
> if(!allocList)
> return;
> for(i = allocList->begin(); i != allocList->end(); i++)
> {
> if((*i)->address == addr)
> {
> allocList->remove((*i));
> break;
> }
> }
> };
> void DumpUnfreed()
> {
> AllocList::iterator i;
> DWORD totalSize = 0;
> char buf[1024];
> if(!allocList)
> return;
> for(i = allocList->begin(); i != allocList->end(); i++) {
> sprintf(buf, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d unfreed\n",
> (*i)->file, (*i)->line, (*i)->address, (*i)->size);
> OutputDebugString(buf);
> totalSize += (*i)->size;
> }
> sprintf(buf, "-----------------------------------------------------------\n");
> OutputDebugString(buf);
> sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
> OutputDebugString(buf);
> };
> #ifdef _DEBUG
> void * __cdecl operator new(unsigned int size,const char *file, int
> line)
> {
> void *ptr = (void *)malloc(size);
> AddTrack((DWORD)ptr, size, file, line);
> return(ptr);
> };
> inline void __cdecl operator delete(void *p)
> {
> RemoveTrack((DWORD)p);
> free(p);
> };
> #endif[/color]
 
Not the answer you were looking for? Post your question . . .
196,955 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,955 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors