Strange memory leak
-
//process.h
private:
PROCESSENTRY32 * m_proc;
char * m_filename;
bool m_stringupdated;//process.cpp
Process::Process()
{
m_proc = new PROCESSENTRY32;
m_proc->dwSize = sizeof(PROCESSENTRY32);
m_stringupdated = false;
m_filename = NULL;
}
//process.cpp
Process::~Process()
{
delete m_proc;
if(m_filename)
delete [] m_filename;
}//main
Process * allo = new Process();
allo->~Process();VLD reports:
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x005A2100: 12 bytes ----------
Call Stack:
c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........Visual Leak Detector detected 1 memory leak (48 bytes).
Largest number used: 640 bytes.
Total allocations: 640 bytes.
Visual Leak Detector is now exiting.
The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).I'm freaking out guys I just don't see the leak ,Thanks in advance :) .
-
//process.h
private:
PROCESSENTRY32 * m_proc;
char * m_filename;
bool m_stringupdated;//process.cpp
Process::Process()
{
m_proc = new PROCESSENTRY32;
m_proc->dwSize = sizeof(PROCESSENTRY32);
m_stringupdated = false;
m_filename = NULL;
}
//process.cpp
Process::~Process()
{
delete m_proc;
if(m_filename)
delete [] m_filename;
}//main
Process * allo = new Process();
allo->~Process();VLD reports:
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x005A2100: 12 bytes ----------
Call Stack:
c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........Visual Leak Detector detected 1 memory leak (48 bytes).
Largest number used: 640 bytes.
Total allocations: 640 bytes.
Visual Leak Detector is now exiting.
The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).I'm freaking out guys I just don't see the leak ,Thanks in advance :) .
How many bytes allocated and pointed by m_filename?
-
//process.h
private:
PROCESSENTRY32 * m_proc;
char * m_filename;
bool m_stringupdated;//process.cpp
Process::Process()
{
m_proc = new PROCESSENTRY32;
m_proc->dwSize = sizeof(PROCESSENTRY32);
m_stringupdated = false;
m_filename = NULL;
}
//process.cpp
Process::~Process()
{
delete m_proc;
if(m_filename)
delete [] m_filename;
}//main
Process * allo = new Process();
allo->~Process();VLD reports:
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x005A2100: 12 bytes ----------
Call Stack:
c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........Visual Leak Detector detected 1 memory leak (48 bytes).
Largest number used: 640 bytes.
Total allocations: 640 bytes.
Visual Leak Detector is now exiting.
The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).I'm freaking out guys I just don't see the leak ,Thanks in advance :) .
If the sizeof(PROCESSENTRY32) == 48, then I have found your leak.
-
How many bytes allocated and pointed by m_filename?
-
If the sizeof(PROCESSENTRY32) == 48, then I have found your leak.
-
//process.h
private:
PROCESSENTRY32 * m_proc;
char * m_filename;
bool m_stringupdated;//process.cpp
Process::Process()
{
m_proc = new PROCESSENTRY32;
m_proc->dwSize = sizeof(PROCESSENTRY32);
m_stringupdated = false;
m_filename = NULL;
}
//process.cpp
Process::~Process()
{
delete m_proc;
if(m_filename)
delete [] m_filename;
}//main
Process * allo = new Process();
allo->~Process();VLD reports:
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x005A2100: 12 bytes ----------
Call Stack:
c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........Visual Leak Detector detected 1 memory leak (48 bytes).
Largest number used: 640 bytes.
Total allocations: 640 bytes.
Visual Leak Detector is now exiting.
The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).I'm freaking out guys I just don't see the leak ,Thanks in advance :) .
I cant see any memory leak in the given code.
//main.. comment out the below code
// Process * allo = new Process();
//allo->~Process();Please check memory leak without allocating memory Process.
-
I cant see any memory leak in the given code.
//main.. comment out the below code
// Process * allo = new Process();
//allo->~Process();Please check memory leak without allocating memory Process.
-
//process.h
private:
PROCESSENTRY32 * m_proc;
char * m_filename;
bool m_stringupdated;//process.cpp
Process::Process()
{
m_proc = new PROCESSENTRY32;
m_proc->dwSize = sizeof(PROCESSENTRY32);
m_stringupdated = false;
m_filename = NULL;
}
//process.cpp
Process::~Process()
{
delete m_proc;
if(m_filename)
delete [] m_filename;
}//main
Process * allo = new Process();
allo->~Process();VLD reports:
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x005A2100: 12 bytes ----------
Call Stack:
c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........Visual Leak Detector detected 1 memory leak (48 bytes).
Largest number used: 640 bytes.
Total allocations: 640 bytes.
Visual Leak Detector is now exiting.
The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).I'm freaking out guys I just don't see the leak ,Thanks in advance :) .
You are explicitly calling the destructor. This will free the allocated memory for the members but not the object itself. You should use
delete
instead:delete allo;
Or you may use auto delete (using an additional member var):
Process::~Process()
{
delete m_proc;
if(m_filename) // this is not necessary
delete [] m_filename;
if (m_bAutoDelete)
delete this;
} -
You are explicitly calling the destructor. This will free the allocated memory for the members but not the object itself. You should use
delete
instead:delete allo;
Or you may use auto delete (using an additional member var):
Process::~Process()
{
delete m_proc;
if(m_filename) // this is not necessary
delete [] m_filename;
if (m_bAutoDelete)
delete this;
}that pretty much solved me the problem something to note delete still calls the destructor. so the auto delete code that you provided will cause an exception.
Process::~Process()
{
if(m_bDeallocated)
return;delete m\_proc; delete \[\] m\_filename; m\_bDeallocated = true; if (m\_bAutoDelete) delete this; //will call this destructor again and cause trouble
}
-
that pretty much solved me the problem something to note delete still calls the destructor. so the auto delete code that you provided will cause an exception.
Process::~Process()
{
if(m_bDeallocated)
return;delete m\_proc; delete \[\] m\_filename; m\_bDeallocated = true; if (m\_bAutoDelete) delete this; //will call this destructor again and cause trouble
}
Yes, you are right. That would result in a recursion. My code lacks resetting the m_bAutoDelete variable. It must be:
Process::~Process()
{
if (m_bAutoDelete)
{
m_bAutoDelete = false;
delete this;
}
else
{
delete m_proc;
delete [] m_filename;
m_proc = NULL;
m_filename = NULL;
}
}However, this is not a good solution because the destructor is called twice and this will fail if the object wasn't allocated using
new
. -
//process.h
private:
PROCESSENTRY32 * m_proc;
char * m_filename;
bool m_stringupdated;//process.cpp
Process::Process()
{
m_proc = new PROCESSENTRY32;
m_proc->dwSize = sizeof(PROCESSENTRY32);
m_stringupdated = false;
m_filename = NULL;
}
//process.cpp
Process::~Process()
{
delete m_proc;
if(m_filename)
delete [] m_filename;
}//main
Process * allo = new Process();
allo->~Process();VLD reports:
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x005A2100: 12 bytes ----------
Call Stack:
c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........Visual Leak Detector detected 1 memory leak (48 bytes).
Largest number used: 640 bytes.
Total allocations: 640 bytes.
Visual Leak Detector is now exiting.
The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).I'm freaking out guys I just don't see the leak ,Thanks in advance :) .