Memory issue in C #, common use if a dll is called by a .Net application, but astronomical if a dll is called from an old application

I created a dll in C # 3.5 that does some memory intensive computation where matrices are created and located a lot. Everything works fine if the dll is called from a .Net application, but if it is called from C ++ the memory usage of the application is just hoisted until the function using matrices is executed. I think there is a problem with automatic garbage collection.

+2


source to share


2 answers


You are most likely not "freeing" the .Net object wrapper references from your unmanaged application. The GC cannot collect this memory unless all references (including external ones) are freed.



+5


source


Deployment on logical request.

The CLR can only garbage collect memory that is allocated and owned by managed code. He does not see or understand unmanaged memory. In the case of C ++ calling a DLL DLL via COM interop, you have a mix of managed and unmanaged memory. Also, you have C ++ objects that keep managed objects alive (via COM pointers and CCW).



The CLR will properly release managed objects, but only when references to them no longer exist. When C ++ accesses a managed object, it creates a CCW under the hood that is supported by the living C ++ COM interface language. Until the reference count of this pointer reaches 0 through the Release and Add calls, it will retain the CCW, and therefore the underlying managed object.

Try to wrap all the places in C ++ where you access the managed object with CComPtr and see if that takes care of your problem.

0


source







All Articles