canvas does not allow drawing

I am double-buffering a bitmap and draw it on a form's canvas. It works fine without flicker, but after running my program for a while, I suddenly get the "canvas does not allow draing" error message. how do I solve this?
Who is Participating?
AlexVirochovskyConnect With a Mentor Commented:
I try you exe and get error:
borlnmm.dll missing.
But if I have get all dll, without source text I can't make debug!
And I think, that debug it is you
part. My part: make advice.
I think, that you must test punkt 3 from
my previous remark.
Use try /catch for find exect place
of error. In this moment test
all ID and arrays.

Other way: make intermediate printing
in file during every Paint and see
values , may be find some strange

And test system resources,may be
in some other part of text
you make "new" without "free"
or go out of array bound.

I am guessing that perhaps, you have done of  a few different things.

1. you have not freed the offscreen bitmaps, thus depleting GDI resources.

2. You have locked the form's canvas.

any of this sound likely?

good luck!!
TropicalFishAuthor Commented:
Yes, I havn't freed the bitmaps. Could you please tell me how to do this (with code). I am using one bitmap as background, and another one that I constantly load different images from a resource into. I draw that bitmap on the background image several times, before I draw the background on the form's canvas. This works for about 100 screen updates, but then the memory probably runs out. How do I free it? Thanks.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  what incarnation of C++ are you using? (C++ Builder, VC++, etc..?);
TropicalFishAuthor Commented:
I'm using C++ Builder 4.
Assuming you are using TBitmaps (and I am assuming that you are in BCB), your loop would look like this:

bmp =new TBitmap;
 //drawing code goes here


Put this in your loop and you should be fine..

Good luck!!
TropicalFishAuthor Commented:
It sais: E2316 'free' is not a member of 'TBitmap'.
TropicalFishAuthor Commented:
Ok, now I'm using "bmp->Free()" to free all my bitmaps, but I still get the "wrong parameter" and "canvas does not allow drawing" after a while, plus now I also get a Win32 error code: 2. I'm confused.
This exception is raised when an attempt is made to use the Handle
property of the Canvas when its handle is still invalid. That is, the
Canvas does not correspond to a valid device context. For example, the
following will raise the EInvalidOperation exception...

#include <memory>

std::auto_ptr<TCanvas> TempCanvas(new TCanvas());

Since most TCanvas methods wrap a GDI function, and nearly all GDI
functions require a handle to a valid device context, simple operations
such as the following will also fail...

std::auto_ptr<TCanvas> TempCanvas(new TCanvas());


std::auto_ptr<TCanvas> TempCanvas(new TCanvas());
TempCanvas->Brush->Color = clRed;

The following however, will work, since the Handle property of the
TempCanvas is specified as the handle to the device context of a valid
windowed control...

std::auto_ptr<TCanvas> TempCanvas(new TCanvas());
TempCanvas->Handle = GetDC(Button1->Handle);
ReleaseDC(Button1->Handle, TempCanvas->Handle);

For test your situation, post snippet
of you code.

TropicalFishAuthor Commented:

Graphics::TBitmap *lcd = new Graphics::TBitmap;
Graphics::TBitmap *knappbild = new Graphics::TBitmap;

Graphics::TBitmap *background = new Graphics::TBitmap;

Graphics::TBitmap *backbuffer = new Graphics::TBitmap;

backbuffer->Width = background->Width;
backbuffer->Height = background->Height;
lcd->Transparent = true;
knappbild->Transparent = true;

void __fastcall TForm1::FormPaint(TObject *Sender)

lcd->LoadFromResourceID((int)HInstance,MarkPos + 1300);

if (FallPos < 4){
lcd->LoadFromResourceID((int)HInstance,FallPos + 1304);

if (FallPos != 0 && FallPos != 3) {lcd->LoadFromResourceID((int)HInstance,UpPos + 1500);
if (FallPos != 1 && FallPos != 2) {lcd->LoadFromResourceID((int)HInstance,NedPos + 1600);
lcd->LoadFromResourceID((int)HInstance,ental + 1200);
if (tiotal << 0 || hundratal << 0) {lcd->LoadFromResourceID((int)HInstance,tiotal + 1200);
if (hundratal << 0) {lcd->LoadFromResourceID((int)HInstance,hundratal + 1200);

if (Miss > 0) {lcd->LoadFromResourceID((int)HInstance,Miss + 1210);

if (knpner[0] == 1) backbuffer->Canvas->Draw(32,158,knappbild);
if (knpner[1] == 1) backbuffer->Canvas->Draw(32,202,knappbild);
if (knpner[2] == 1) backbuffer->Canvas->Draw(314,203,knappbild);
if (knpner[3] == 1) backbuffer->Canvas->Draw(314,159,knappbild);

I have some remarks to you code:
1. After using must free! as
void __fastcall TSwatForm::FormDestroy(TObject *Sender)
    delete lcd;
    delete backbuffer ;
2. Test MarkCord,.., FallCord arrays:
   may by values are invalid
3. You use LoadFromResourceID function
   if ID (as NedPos + 1600) is invalid
: no resource with this ID, Draw fails
 Test all resources, espesially before
 error message!
4.  What meens
>>  if (hundratal << 0) ?
it is same as
if (hundratal)

5. And last : i don't like Load some
   bitmap during paint!
   Better way is : load during changings
   parameters (as FallPos for example),
   and paint deja loaded Bitmaps.


TropicalFishAuthor Commented:
Um.. it seems that "if(hundratal << 0)" does the same thing as "if(hundratal > 0)". Work both ways.. means bigger than 0. The MarkCord and FallCord arrays are fine, and all resources work up till the error point. I'll look into your points 1 and 5 though.

Also, I have put up my executable at:
If you have the time, maybe you want to take a look at it, it's a 307kb download. Start the game on gameA and play it with q,s,8,4 keys up till about 30 points (sometimes longer) Then you'll see the errors.
TropicalFishAuthor Commented:
Adjusted points from 50 to 75
TropicalFishAuthor Commented:
Now I have changed it so no bitmaps are loaded during paint, like you said. No difference. Only that I need to use 12 bitmaps instead of four, otherwise it writes over itself before drawing it to the backbuffer. I increased the points for this question, I really need this to workout!
All Courses

From novice to tech pro — start learning today.