简体   繁体   中英

Avoid memory leak in c# client of a unmanaged DLL with memory leak

Unfortunately, I have to use in a C# client a native C++ DLL with memory leaks .

I'd to know if there is a good way to avoid having this memory leak in my c# app ?

Actually, I try to load/unload many times my DLL to avoid memory occupation but i am not sure that it works to avoid memory leak :

// Dynamically load DLL file
NativeMethods.LoadLibrary(...)

// Do stuffs with the DLL
... (call compute method)

// Dynamically unload DLL file
NativeMethods.FreeLibrary(...)

// Dynamically load DLL file
NativeMethods.LoadLibrary(...)

// Do stuffs with the DLL
... (call compute method)

// Dynamically unload DLL file
NativeMethods.FreeLibrary(...)

etc...

Thanks

According to my theory, this changes nothing. In C#/managed, nothing much different will happen than if you'd load a DLL into a native program. The DLL will allocate heap memory and since that resource belongs to the process it will persist, even if the DLL is unloaded.

Things would change if you'd have a purely managed DLL. You could then load it into an app domain and dispose the DLL alltogether with the App-Domain...

So solutions? The hard way would be similar to the 64-Bit Process with 32-Bit DLL Problem. Create a separate process, load the DLL and do remote calls. This is cumbersome but if it is only a vew calls, it may be worth it.

Other: Not that I know of but fixing the DLL's code.

If you use any third party dll in c#, we have to manually remove when it not use

By manually remove object reference using following command. GC.SuppressFinalize()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM