dwmkerr / sharpshell

SharpShell makes it easy to create Windows Shell Extensions using the .NET Framework.
MIT License
1.52k stars 260 forks source link

Context Menus leaks GDI objects. #135

Open dosh1974 opened 7 years ago

dosh1974 commented 7 years ago

Every time a Context Menu is invoked (right clicking on a Shell Object), the number of GDI objects used by explorer.exe increases. Eventually it fails to create more objects and the whole menu becomes black (or the explorer process terminates).

In my use case the number of objects increase by roughly 400 objects per invocation (yes we do have some complex menus). I've noticed that if I do NOT have any bitmaps on my menu items the usage increase is fairly small (about 3 objects). I've tracked down and fixed one leak in the source and that cut the leaks down to about half (200 in my case). I've further added a bitmap cache (.NET bitmap to native Bitmap handle), this cut the number of leaks down to about 100 (which is probably about the number of Items with a Bitmap in my case). This seems to have sped up the menu creation a lot as well.

Since I'm not very intimate with the whole Shell Extension concept I'm a little bit lost as to where to go from here? Who is responsible for destroying the created menu? Something is clearly not destroyed correctly.

On the .NET side there doesn't seem to exist any "dispose" hooks, and none of the SharpShell classes implements the IDisposable interface. In my implementation the Bitmaps are statically created once (and never released), but I've tried to tie them to an instance and Dispose them in the finalizer without any further success, so I still think the leak is on the unmanaged side of things.

I also spotted and fixed another bug which doesn't seem to affect GDI object usage, plus a bunch of other things I tried out.

Patch diff attached: SharpShell.diff.txt

DarkCreekWay commented 5 years ago

Did the changes made it into master ?