0

I have a dll Native method calling from my .Net c# code. Every time the method is invoked it is occupying the Windows memory space. At one point of time my code is failing due to insufficient memory. How to free the Windows memory space?

1 Answers1

1

The native method should give you access to a method for freeing the memory it allocated, or it should tell you how it was allocated and how to deallocate it.

In general, if a method returns a pointer (or a string, or a handle! They are both pointers), you should always first ask yourself how will I free this memory? If you don't ask yourself this question, you are probably generating a memory leak.

Then you should watch with suspect structures ("objects") built by the library that contain strings or pointers to other objects, and be sure that the .NET marshaller is able to free the memory when necessary.

In general, handling the memory of pinvokes is complex! It requires much care, even because something could be hidden to you by the marshaller.

See https://stackoverflow.com/a/18172997/613130 and https://stackoverflow.com/a/29508809/613130

then there is always the possibility that the memory leak is caused by faulty code dll-side, nothing to do then, other than correct the bug in the dll code/contact whoever wrote it

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280