Hello.
I'm currently having a memory leak problem in my program (C++ with MFC, VS2005) and I don't know WHY it happens, as the code seems to be OK to me. Anyway, here's the code that causes memory leaks:
char* va( char* FormatStr, ... )
{
va_list ArgPtr;
char *FinalString;
int length;
va_start( ArgPtr, FormatStr );
length = _vscprintf( FormatStr, ArgPtr )+1;
FinalString = new char[length];
vsprintf_s( FinalString, length, FormatStr, ArgPtr );
va_end( ArgPtr );
return FinalString;
}
void CMFC1Dlg::OnBnClickedButton1()
{
if( m_POpened )
{
CString cmd;
m_EditCmd.GetWindowTextA( cmd );
m_Port.WriteToPort( va( "%s%c", cmd, 13 ) );
}
}
I have searched the forums already, but found no solution to this... These are the leaks (assuming cmd = "AT"
):
The thread 'Win32 Thread' (0x9a4) has exited with code 100 (0x64).
The thread 'Win32 Thread' (0x6b0) has exited with code 0 (0x0).
Detected memory leaks!
Dumping objects ->
e:\dawg\prg\vs2005\c++\mfc1\mfc1\mfc1\mfc1.cpp(102) : {140} normal block at 0x003B9ED0, 4 bytes long.
Data: <AT > 41 54 0D 00
e:\dawg\prg\vs2005\c++\mfc1\mfc1\mfc1\mfc1.cpp(102) : {112} normal block at 0x003B9BB8, 4 bytes long.
Data: <AT > 41 54 0D 00
e:\dawg\prg\vs2005\c++\mfc1\mfc1\mfc1\mfc1.cpp(102) : {110} normal block at 0x003B3828, 4 bytes long.
Data: <AT > 41 54 0D 00
Object dump complete.
The program '[2636] MFC1.exe: Native' has exited with code 0 (0x0).
Debugger points to FinalString = new char[length];
as the source of the leak. Could you please help me out?
Kind regards
Pat