views:

38

answers:

1

I'm using Visual Studio and while fiddling around with a custom control, I started to get an out of memory error.

alt text

The stack trace is not helpful - it's just a bunch of frames doing Reflection and so on.

alt text

How can I fix this?

A: 

There was a problem in the constructor of the custom control.

For some reason, calling Bitmap.MakeTransparent() on a bitmap that doesn't have a transparent bit set, causes an out of memory error.

This was being reported in the designer, but without any of the actually relevant stack trace.

The solution was to comb through the custom control and make sure the constructor could never through an exception. One approach is to surround the constructor logic in the control with a try...catch, and report the error with Console.WriteLine. You'll see it at runtime, with full stack trace.


Note: I searched the intertubes and saw a number of questions asking about similar problems, but nothing definitive for an answer. So I put this question + answer here hoping it might help someone else.

Cheeso