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

memory is allocated on line 10 but never deleted. Try this:

void CMFC1Dlg::OnBnClickedButton1()
{
        char *ptr;
	if( m_POpened )
	{
		CString cmd;
		m_EditCmd.GetWindowTextA( cmd );
                ptr = va( "%s%c", cmd, 13 );
		m_Port.WriteToPort( ptr);
                delete[] ptr;
	}
}

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

Yes, every time the 'va' function executes it allocates new memory, and returns a pointer to the newly allocated memory, but you don't delete the assigned memory when you don't need it anymore and that causes the memory leak :) ...

Thank you very much! I forgot to free up the memory indeed.
But this way, I won't be able to use va() inline... I have to declare a pointer and delete it every time I want to use va(). Or is there some other way around, like freeing up the memory in va()?

Or is there some other way around, like freeing up the memory in va()?

In the code you post this will lead to undefined behavior, as you're returning a pointer which points to deallocated memory, that's very dangerous :)

Well, it looks like my dream of an inline variable argument function is gone...

Thank you for your help! :)

you could do this:

void CMFC1Dlg::OnBnClickedButton1()
{
        char *ptr;
	if( m_POpened )
	{
		CString cmd;
		m_EditCmd.GetWindowTextA( cmd );
		m_Port.WriteToPort( (ptr = va( "%s%c", cmd, 13 )));
                delete[] ptr;
	}
}

or this:

const char* va( char* FormatStr, ... )
{
	va_list ArgPtr;
	static char FinalString[2048];
	va_start( ArgPtr, FormatStr );
	vsprintf_s( FinalString, sizeof(FinalString), FormatStr, ArgPtr );
	va_end( ArgPtr );
	return FinalString;
}

or this:

const char* va( char* FormatStr, ... )
{
	va_list ArgPtr;
	static char FinalString[2048];
	va_start( ArgPtr, FormatStr );
	vsprintf_s( FinalString, sizeof(FinalString), FormatStr, ArgPtr );
	va_end( ArgPtr );
	return FinalString;
}

Sir, you are my saviour! Thank you very much, it's much better this way.

Thank you and have a good day! ;)

This question has already been answered. Start a new discussion instead.