By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

How to find Memory Leak

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
If you are using the MFC part of it then you can use
CMemoryState object to find memory leaks ....

Amol

sr*********@hotmail.com (Harsha) wrote in message news:<ca**************************@posting.google. com>...
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

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.