Connecting Tech Pros Worldwide Help | Site Map

How to find Memory Leak

  #1  
Old July 19th, 2005, 09:13 PM
Harsha
Guest
 
Posts: 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
  #2  
Old July 19th, 2005, 09:14 PM
Amol Chavan
Guest
 
Posts: n/a

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

sree_harsha@hotmail.com (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]
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory Leak Peter answers 22 July 17th, 2008 08:25 PM
Memory leak advice needed Boni answers 13 November 23rd, 2005 04:47 AM
How to find memory leak Tosch answers 1 November 21st, 2005 03:01 AM
How to find memory leaks? Divick answers 3 September 21st, 2005 06:45 AM