After some reading I found out that everything you code in C# is managed code, and shouldn't give memory leaks. Yet my program behaves in a manner which suggests some sort of memory leak.
My entire program comes down to this: (currently I don't possess the actual code)
while(true)
{
//Source of the "leak"
List<object> _objects = ReturnAllWindows();
//Do something awesome with the list...
System.Threading.Thread.Sleep(10);
}
ReturnAllWindows
is a method that uses pinvoke
in combination with user32.dll
and EnumWindows
to get all the windows that are currently open.
When I run my program, the memory immediately skyrockets until I get an OutOfMemoryException
.
From what I read, the only thing I can think of is that there is some sort of memory leak in the EnumWindows
function, but I can hardly imagine that user32 isn't fully managed.
So what is going on? And how can I prevent / work around it?
EDIT: Solved it, the problem was that this list is later used in combination with some multithreading which wasn't handled properly. If you comment the
ReturnAllWindows
line, the multithreading was never reached, the platform invoke wasn't the problem at all.