Home
Reply
Regular Contributor
xpz
Posts: 46

memory leak in using interop

When we test the sample code in the interop r19 sp3, we run the example Simple_Mbcs_vc8, but when we add following memory leak detection code, the visual studio report there are huge memory leak in the example.

-------------------------------------------------------------------------------------------------

#include <crtdbg.h>

 

// add memory dump before the main()  "return result;"

_CrtDumpMemoryLeaks();

-------------------------------------------------------------------------------------------------

 

Now, we have question, whether the interop really has memory leak (or the visual studio report the fake message - the interop use late-memory release technique to release memory after the application end).

 

If no leak, how to avoid visual studio to report the fake message (we still need this code to detect the memory leak for our application)

 

 

Thanks in advance.

 

Spatial Moderator
ybiyani
Posts: 223

Re: memory leak in using interop

Regular Contributor
xpz
Posts: 46

Re: memory leak in using interop

Get it, thank you very much.

Contributor
PaulS
Posts: 16

Re: memory leak in using interop

[ Edited ]

If you managed to remove the memory leaks, would you please share your solution?  For even the most trivial application I am unable to remove 8 leaks, no matter what combination of #pragma, additional dependencies, etc. I try!  

 

I have read and reread the page "Compiler_Flags_and_Preprocessor_Directives#Understanding_False_Memory_Leaks_Reported_in_ACIS-based_MFC_Applications" and obviously not understood it!  Apologies for the long email, but maybe the details will enable someone who does understand that page to spot where I am going wrong and suggest a fix.

 

I have a testbed Win32 console app complied in VS2008 against InterOp 20_0_3.  To match our real application, the testbed is configured to "Use MFC in a shared DLL", and "Dynamic link to ATL".  

 

The testbed merely instantiates a SPAIConverter object which goes out of scope immediately:

 

#define SPA_NO_AUTO_LINK


#pragma comment( lib, "msvcrtd.lib" )

#pragma comment( lib, "msvcurtd.lib" )

#pragma comment( lib, "msvcprtd.lib" )


#pragma comment( lib, "SPAXInteropd.lib" )

#pragma comment( lib, "SPAXBased.lib" )

#pragma comment( lib, "SpaACISd.lib" )


#include <tchar.h>

#include <crtdbg.h>

#include "SPAIConverter.h"


int _tmain(int argc, _TCHAR* argv[])

{

    int tmp = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

    _CrtSetDbgFlag( tmp | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );


    {

        SPAIConverter Converter;

    }


    return 0;

}

 

 

I get:

 

{134} normal block at 0x00383F58, 256 bytes long. Data: <  G   G         > 10 04 47 00 10 0B 47 00 00 00 00 00 00 00 00 00 

{130} normal block at 0x00388028, 12252 bytes long. Data: <                > 01 00 00 80 00 00 00 00 00 00 00 00 01 00 00 80 

{129} normal block at 0x00383D40, 56 bytes long. Data: <( 8  %I  (I     > 28 80 38 00 00 25 49 00 C0 28 49 00 00 00 00 00 

{128} normal block at 0x00383C20, 228 bytes long. Data: <         ;8     > E4 00 00 00 01 00 00 00 C8 3B 38 00 00 00 00 00 

{127} normal block at 0x00383BC8, 28 bytes long. Data: <     z4K        > 00 00 00 00 F0 7A 34 4B 00 00 00 00 01 00 00 00 

{126} normal block at 0x00383B70, 28 bytes long. Data: <     ' J        > 00 00 00 00 20 27 E1 4A 00 00 00 00 01 00 00 00 

{125} normal block at 0x00383B18, 28 bytes long. Data: < dP    J   J    > 9C 64 50 00 00 10 D0 4A 80 10 D0 4A 01 00 00 00 

Object dump complete.

 

If I "new" a char at global scope and neglect to delete it, It appears as allocation {9311}.  This allocation number is very much higher than those of the leaks above.

 

If I break at the global allocation, here are the libraries loaded:

 

'MemoryLeaks.exe': Loaded 'E:\Not My Documents\My Projects\Testbeds\InterOp\MemoryLeaks\Debug\MemoryLeaks.exe', Symbols loaded.

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.4148_x-ww_5d84dd2f\msvcr90d.dll', Symbols loaded.

'MemoryLeaks.exe': Loaded 'E:\Projects\5226\InterOp\bin\NT_VC9_DLLD\SPAXInteropd.dll'

'MemoryLeaks.exe': Loaded 'E:\Projects\5226\InterOp\bin\NT_VC9_DLLD\icuuc38d.dll'

'MemoryLeaks.exe': Loaded 'E:\Projects\5226\InterOp\bin\NT_VC9_DLL\icudt38.dll', Binary was not built with debug information.

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\secur32.dll'

'MemoryLeaks.exe': Loaded 'E:\Projects\5226\InterOp\bin\NT_VC9_DLLD\SPAXBased.dll'

'MemoryLeaks.exe': Loaded 'E:\Projects\5226\InterOp\bin\NT_VC9_DLLD\SpaACISd.dll', Symbols loaded.

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\netapi32.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\iphlpapi.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\user32.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\ws2_32.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\ws2help.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\imm32.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\lpk.dll'

'MemoryLeaks.exe': Loaded 'C:\WINDOWS\system32\usp10.dll'

 

 

Paul Scully
Senior Software Engineer
Renishaw PLC

T +44 (0)1453 524764
F +44 (0)1453 523201
E paul.scully@renishaw.com
www.renishaw.com
Contributor
PaulS
Posts: 16

Re: memory leak in using interop

[ Edited ]

Running the testbed exe through "Dependency Walker" shows the following profile:

 

(Attached full profile).  Below are the unloads.

 

...

00:00:00.562: DllMain(0x629C0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\LPK.DLL" called.

00:00:00.562: DllMain(0x629C0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\LPK.DLL" returned 1 (0x1).

00:00:00.562: DllMain(0x74D90000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\USP10.DLL" called.

00:00:00.562: DllMain(0x74D90000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\USP10.DLL" returned 1 (0x1).

00:00:00.578: DllMain(0x76390000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\IMM32.DLL" called.

00:00:00.578: DllMain(0x76390000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\IMM32.DLL" returned 1 (0x1).

00:00:00.578: DllMain(0x10000000, DLL_PROCESS_DETACH, 0x00000001) in "e:\projects\5226\interop\bin\nt_vc9_dlld\SPAXINTEROPD.DLL" called.

00:00:00.578: DllMain(0x10000000, DLL_PROCESS_DETACH, 0x00000001) in "e:\projects\5226\interop\bin\nt_vc9_dlld\SPAXINTEROPD.DLL" returned 1 (0x1).

00:00:00.578: DllMain(0x006C0000, DLL_PROCESS_DETACH, 0x00000001) in "e:\projects\5226\interop\bin\nt_vc9_dlld\SPAACISD.DLL" called.

00:00:00.593: DllMain(0x006C0000, DLL_PROCESS_DETACH, 0x00000001) in "e:\projects\5226\interop\bin\nt_vc9_dlld\SPAACISD.DLL" returned 1 (0x1).

00:00:00.593: DllMain(0x76D60000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\IPHLPAPI.DLL" called.

00:00:00.593: DllMain(0x76D60000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\IPHLPAPI.DLL" returned 1 (0x1).

00:00:00.593: DllMain(0x71AB0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\WS2_32.DLL" called.

00:00:00.593: DllMain(0x71AB0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\WS2_32.DLL" returned 1 (0x1).

00:00:00.593: DllMain(0x71AA0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\WS2HELP.DLL" called.

00:00:00.593: DllMain(0x71AA0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\WS2HELP.DLL" returned 1 (0x1).

00:00:00.593: DllMain(0x7E410000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\USER32.DLL" called.

00:00:00.593: DllMain(0x7E410000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\USER32.DLL" returned 1 (0x1).

00:00:00.593: DllMain(0x77F10000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\GDI32.DLL" called.

00:00:00.593: DllMain(0x77F10000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\GDI32.DLL" returned 1 (0x1).

00:00:00.593: DllMain(0x5B860000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\NETAPI32.DLL" called.

00:00:00.593: DllMain(0x5B860000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\NETAPI32.DLL" returned 2147348481 (0x7FFDF001).

00:00:00.593: DllMain(0x77C10000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\MSVCRT.DLL" called.

00:00:00.593: DllMain(0x77C10000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\MSVCRT.DLL" returned 1 (0x1).

00:00:00.593: DllMain(0x00560000, DLL_PROCESS_DETACH, 0x00000001) in "e:\projects\5226\interop\bin\nt_vc9_dlld\SPAXBASED.DLL" called.

00:00:00.609: DllMain(0x00560000, DLL_PROCESS_DETACH, 0x00000001) in "e:\projects\5226\interop\bin\nt_vc9_dlld\SPAXBASED.DLL" returned 1 (0x1).

00:00:00.609: DllMain(0x00420000, DLL_PROCESS_DETACH, 0x00000001) in "e:\projects\5226\interop\bin\nt_vc9_dlld\ICUUC38D.DLL" called.

00:00:00.609: DllMain(0x00420000, DLL_PROCESS_DETACH, 0x00000001) in "e:\projects\5226\interop\bin\nt_vc9_dlld\ICUUC38D.DLL" returned 1 (0x1).

00:00:00.609: DllMain(0x77DD0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\ADVAPI32.DLL" called.

00:00:00.609: DllMain(0x77DD0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\ADVAPI32.DLL" returned 1 (0x1).

00:00:00.609: DllMain(0x77E70000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\RPCRT4.DLL" called.

00:00:00.609: DllMain(0x77E70000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\RPCRT4.DLL" returned 1 (0x1).

00:00:00.609: DllMain(0x77FE0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\SECUR32.DLL" called.

00:00:00.609: DllMain(0x77FE0000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\SECUR32.DLL" returned 1 (0x1).

00:00:00.609: DllMain(0x10200000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.4148_x-ww_5d84dd2f\MSVCR90D.DLL" called.

00:00:00.609: Detected memory leaks!

00:00:00.609: Dumping objects ->

00:00:00.609: {9299} normal block at 0x0038F698, 1 bytes long.

00:00:00.625:  Data: < > CD 

00:00:00.625: {128} normal block at 0x00389D10, 256 bytes long.

00:00:00.625:  Data: <  G   G         > 10 04 47 00 10 0B 47 00 00 00 00 00 00 00 00 00 

00:00:00.625: {124} normal block at 0x00386B58, 12252 bytes long.

00:00:00.625:  Data: <                > 01 00 00 80 00 00 00 00 00 00 00 00 01 00 00 80 

00:00:00.625: {123} normal block at 0x00386AE0, 56 bytes long.

00:00:00.625:  Data: <Xk8  %I  (I     > 58 6B 38 00 00 25 49 00 C0 28 49 00 00 00 00 00 

00:00:00.625: {122} normal block at 0x003869C0, 228 bytes long.

00:00:00.625:  Data: <        hi8     > E4 00 00 00 01 00 00 00 68 69 38 00 00 00 00 00 

00:00:00.625: {121} normal block at 0x00386968, 28 bytes long.

00:00:00.625:  Data: <     z4K        > 00 00 00 00 F0 7A 34 4B 00 00 00 00 01 00 00 00 

00:00:00.625: {120} normal block at 0x00386910, 28 bytes long.

00:00:00.625:  Data: <     ' J        > 00 00 00 00 20 27 E1 4A 00 00 00 00 01 00 00 00 

00:00:00.625: {119} normal block at 0x003868B8, 28 bytes long.

00:00:00.625:  Data: < dP    J   J    > 9C 64 50 00 00 10 D0 4A 80 10 D0 4A 01 00 00 00 

00:00:00.625: Object dump complete.

00:00:00.625: DllMain(0x10200000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.4148_x-ww_5d84dd2f\MSVCR90D.DLL" returned 1 (0x1).

00:00:00.640: DllMain(0x08370000, DLL_PROCESS_DETACH, 0x00000001) in "c:\progra~1\depends\DEPENDS.DLL" called.

00:00:00.640: DllMain(0x08370000, DLL_PROCESS_DETACH, 0x00000001) in "c:\progra~1\depends\DEPENDS.DLL" returned 1 (0x1).

00:00:00.640: DllMain(0x7C800000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\KERNEL32.DLL" called.

00:00:00.640: DllMain(0x7C800000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\KERNEL32.DLL" returned 1 (0x1).

00:00:00.640: DllMain(0x7C900000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\NTDLL.DLL" called.

00:00:00.640: DllMain(0x7C900000, DLL_PROCESS_DETACH, 0x00000001) in "c:\windows\system32\NTDLL.DLL" returned 1 (0x1).

00:00:00.640: Exited "e:\not my documents\my projects\testbeds\interop\memoryleaks\debug\MEMORYLEAKS.EXE" (process 0xF68) with code 0 (0x0).

 

Note after the point where the leaks are reported, only system DLLs are unloaded - all InterOp dlls have been unloaded, and should not be retaining any memory (...except leaks?)
Paul Scully
Senior Software Engineer
Renishaw PLC

T +44 (0)1453 524764
F +44 (0)1453 523201
E paul.scully@renishaw.com
www.renishaw.com