"bitmap can't be selected in another DC" assertion Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
soundhound
Knows some wx things
Knows some wx things
Posts: 28
Joined: Tue Jun 19, 2018 1:05 pm

"bitmap can't be selected in another DC" assertion

Post by soundhound »

I'm hitting the "bitmap can't be selected in another DC" assertion in bitmap.cpp
and after putting in some isOk()'s am none the wiser what's causing it.

I'm actually translating the distribution samples/drawing.cpp through another language binding to the wxw libraries
and because it's quite faithful to the original and the original runs fine (under g++ compiler),
it's strange.

the only thing I can think of is that because of the garbage collector of the translated-to language
there will be undeleted objects floating around potentially long after they have gone out of source scope.

or the sample is flawed and the translation somehow forces the issue!

here's the C++ snippet causing the problem (but only in the other language binding):
wxImage img(21, 21, false);
img.Clear(1);
wxBitmap bmp(img);
{
wxMemoryDC mdc(bmp); <--------------------------------- if this is skipped then no assertion
.....
}
bmp.SetMask(new wxMask(bmp, wxColour(1, 1, 1))); <------------- this is where the assert happens if the statement above is also executed
dc.DrawBitmap(bmp, -10, -10, true);

is there something incomplete about the handling of the bitmap?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: "bitmap can't be selected in another DC" assertion

Post by PB »

The documentation clearly states
...before performing any other operations on the bitmap data, the bitmap must be selected out of the memory DC
Did you try that, i.e., calling

Code: Select all

mdc.SelectObject(wxNullBitmap);
before using bmp?
soundhound
Knows some wx things
Knows some wx things
Posts: 28
Joined: Tue Jun 19, 2018 1:05 pm

Re: "bitmap can't be selected in another DC" assertion

Post by soundhound »

ah yes!
I thought as it was written by a lead wxw developer in the day it must have been ok as it was a canonical example.
which contradicts my suspicion there was something with the original code :roll:
and then I found some other places where it was missing although there weren't any apparent problems at those sites.
but all good now.
thank you!