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?
TropicalFishAsked:
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.

Alex
0
 
DrDelphiCommented:
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!!
0
 
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.
0
Never miss a deadline with monday.com

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

 
DrDelphiCommented:
Trop,
  what incarnation of C++ are you using? (C++ Builder, VC++, etc..?);
0
 
TropicalFishAuthor Commented:
I'm using C++ Builder 4.
0
 
DrDelphiCommented:
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
}

bmp->free;

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


Good luck!!
0
 
TropicalFishAuthor Commented:
It sais: E2316 'free' is not a member of 'TBitmap'.
0
 
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.
0
 
AlexVirochovskyCommented:
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());
ShowMessage(reinterpret_cast<int>(TempCanvas->Handle));


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());
TempCanvas->FillRect(ClientRect);

or,

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);
TempCanvas->FillRect(Button1->ClientRect);
ReleaseDC(Button1->Handle, TempCanvas->Handle);

For test your situation, post snippet
of you code.

Alex
0
 
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;

background->LoadFromResourceID((int)HInstance,1400);
knappbild->LoadFromResourceID((int)HInstance,1401);
backbuffer->Width = background->Width;
backbuffer->Height = background->Height;
lcd->Transparent = true;
knappbild->Transparent = true;


void __fastcall TForm1::FormPaint(TObject *Sender)
{
backbuffer->Canvas->Draw(0,0,background);

lcd->LoadFromResourceID((int)HInstance,MarkPos + 1300);
backbuffer->Canvas->Draw(MarkCord[MarkPos][0],MarkCord[MarkPos][1],lcd);

if (FallPos < 4){
lcd->LoadFromResourceID((int)HInstance,FallPos + 1304);
backbuffer->Canvas->Draw(FallCord[FallPos][0],FallCord[FallPos][1],lcd);
}

if (FallPos != 0 && FallPos != 3) {lcd->LoadFromResourceID((int)HInstance,UpPos + 1500);
                                   backbuffer->Canvas->Draw(UpCord[UpPos],88,lcd);}
if (FallPos != 1 && FallPos != 2) {lcd->LoadFromResourceID((int)HInstance,NedPos + 1600);
                                   backbuffer->Canvas->Draw(NedCord[NedPos],128,lcd);}
lcd->LoadFromResourceID((int)HInstance,ental + 1200);
backbuffer->Canvas->Draw(216,71,lcd);
if (tiotal << 0 || hundratal << 0) {lcd->LoadFromResourceID((int)HInstance,tiotal + 1200);
                                    backbuffer->Canvas->Draw(201,71,lcd);}
if (hundratal << 0) {lcd->LoadFromResourceID((int)HInstance,hundratal + 1200);
                     backbuffer->Canvas->Draw(180,71,lcd);}

if (Miss > 0) {lcd->LoadFromResourceID((int)HInstance,Miss + 1210);
               backbuffer->Canvas->Draw(230,70,lcd);}

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);

Form1->Canvas->Draw(0,0,backbuffer);
}
0
 
AlexVirochovskyCommented:
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.

Alex

0
 
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:
http://w1.152.telia.com/~u15205559/Manhole05b.zip
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.
Thanks.
0
 
TropicalFishAuthor Commented:
Adjusted points from 50 to 75
0
 
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!
0
All Courses

From novice to tech pro — start learning today.