I used Visual Studio's tools for reporting memory leaks.

Main program uses a dll. In main program it reports the file name and line number:


d:\tmp\ali\ali\ali.cpp(47) : {200} client block at 0x003A84C0, subtype 0, 5000 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD

but in dll it doesn't:

{199} normal block at 0x003AC018, 10000 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD


This style reporting isn't usefull for me.

What's wrong here? How can i fix this problem.

thanks!!


Exe Source Code:

#include "stdafx.h"

#ifdef _DEBUG
   #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
   #define DEBUG_CLIENTBLOCK
#endif // _DEBUG

#if defined(_DEBUG) && defined(WIN32)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

#include <windows.h>
#include <conio.h>


#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif



int _tmain(int argc, _TCHAR* argv[])
{
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	HINSTANCE m_kutuphane = LoadLibrary(L"DebugDLL.dll");

	typedef __declspec(dllimport) void __stdcall MyDllFunction();
        
    char	getDllFunctionName[]="?dllFunction@@YAXXZ";
    
    
    MyDllFunction*  mf = NULL;
    
    mf = (MyDllFunction *) GetProcAddress(m_kutuphane,getDllFunctionName);
    
    
    mf();

    //memory leak
    new char[5000];

    while (!_kbhit());

    return 0;
}


Dll Source Code:

// Check for memory leaks

#if defined(_DEBUG) && defined(WIN32)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{

	//_crtBreakAlloc = 1397;
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    return TRUE;

	new char[10000];
}

__declspec( dllexport ) void dllFunction() 

{
	std::cout << "dll: Hello!" << std::endl;
	//Memory leakage
	new char[10000];
	
}


Output :

'ali.exe': Loaded 'D:\tmp\ali\debug\ali.exe', Symbols loaded.
'ali.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'ali.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'ali.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_5490cd9f\msvcr80d.dll', Symbols loaded.
'ali.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
'ali.exe': Loaded 'D:\tmp\ali\debug\DebugDLL.dll', Symbols loaded.
'ali.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_5490cd9f\msvcp80d.dll', Symbols loaded.
Detected memory leaks!
Dumping objects ->
d:\tmp\ali\ali\ali.cpp(47) : {200} client block at 0x003A84C0, subtype 0, 5000 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
{199} normal block at 0x003AC018, 10000 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
The program '[5988] ali.exe: Native' has exited with code 0 (0x0).



Compiler settings:

Exe:

/Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MDd /Yu"stdafx.h" /Fp"Debug\ali.pch" /Fo"Debug\\" /Fd"Debug\vc80.pdb" /W3 /nologo /c /Wp64 /ZI /TP /errorReport:prompt

Dll:

/Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_USRDLL" /D "DEBUGDLL_EXPORTS" /D "_UNICODE" /D "UNICODE" /D "_WINDLL" /Gm /EHsc /RTC1 /MDd /Yu"stdafx.h" /Fp"Debug\DebugDLL.pch" /Fo"Debug\\" /Fd"Debug\vc80.pdb" /W3 /nologo /c /Wp64 /ZI /TP /errorReport:prompt


Linker settings:

Exe:

/OUT:"D:\tmp\ali\Debug\ali.exe" /INCREMENTAL /NOLOGO /MANIFEST /MANIFESTFILE:"Debug\ali.exe.intermediate.manifest" /DEBUG /PDB:"d:\tmp\ali\debug\ali.pdb" /SUBSYSTEM:CONSOLE /MACHINE:X86 /ERRORREPORT:PROMPT kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

Dll:

/OUT:"D:\tmp\ali\Debug\DebugDLL.dll" /INCREMENTAL /NOLOGO /DLL /MANIFEST /MANIFESTFILE:"Debug\DebugDLL.dll.intermediate.manifest" /DEBUG /PDB:"d:\tmp\ali\debug\DebugDLL.pdb" /SUBSYSTEM:WINDOWS /MACHINE:X86 /ERRORREPORT:PROMPT kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

Recommended Answers

All 2 Replies

You're showing us exactly where the leaks are. You're using the new operator in a wrong way.
You say: new char[1000] which allocates room for a 1000 chars. But you never assign the pointer to anything, and thus you can't delete the memory you've allocated which results in a memory-leak.
Here's a tutorial on dynamic memory

I added below line to dll's code

#define new DEBUG_CLIENTBLOCK

and its working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.