Board index » cppbuilder » Leak of GDI and System resources

Leak of GDI and System resources

/*
The following is the smallest program that demonstrates my problem.
It requires an Image and Timer component on a Form. Count in Form->Caption
Load "Programs/Accessories/System Tools/Resource Meter" to monitor the leak.
Starting from a reboot this program will switch images about 11,000 times
before GDI and system resources begin to drop. They will continue to drop
1% per 360 switches. The lost resources are never recovered without a boot.
I have used MemProof on the actual program(a card game) and ALL the DLL's
used without it finding any leaks.  This was compiled with C++Builder4 under
Win98 on a 400Mhz Pentium II with 256Megs and also tested on Win95 with
similar results. I'm an old hacker from the 70's but a newbie to graphics.
What have I done wrong? tnx.
*/
TMetafile *mfFront, *mfBack;

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TMetafileCanvas *mc;
  //Set pause between images long enough to allow them to be redrawn
  Timer1->Interval=50;
  //Create 1st in memory metafile
  mfFront = new TMetafile;
  mc = new TMetafileCanvas(mfFront, 0);
  mc->Brush->Color=clRed;
  mc->Ellipse(0, 0, Image1->Width, Image1->Height);
  delete mc;
  //Create 2nd in memory metafile
  mfBack = new TMetafile;
  mc = new TMetafileCanvas(mfBack, 0);
  mc->Brush->Color=clBlack;
  mc->Ellipse(0, 0, Image1->Width, Image1->Height);
  delete mc;

Quote
}

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  static int i;
  //Switch metafiles on each tick
  Image1->Picture->Metafile=(i%2)?mfFront:mfBack;
  Form1->Caption=IntToStr(++i);
Quote
}

 

Re:Leak of GDI and System resources


Hi,

Quote
eforbes wrote:
> What have I done wrong? tnx.
> ...
> void __fastcall TForm1::FormCreate(TObject *Sender)

You can try moving the code from the OnCreate event to the constructor of
the form. But I'm not sure if it will help.

Regards,

Micha.

Re:Leak of GDI and System resources


Quote
> The following is the smallest program that demonstrates my problem.

Hi,

If MemProof does not report any GDI resource leaks, then your problem is
either in a driver or in the OS itself. I have specifically heard of Windows
having resource problems with Metafiles, so it is possible that this is the
cause and not the video driver. You might first try with different graphic
card drivers, just in case.

If you can restructure your program to avoid using Metafiles, if should fix
the problem.

Cheers,
  Atanas

Re:Leak of GDI and System resources


Thank you Atanas,
   You were right about Windows leaking!  What a croc.....
I used metafiles because it seemed to give me the sharpest images.  I found
that resizing caused such unacceptable image deterioration that I redrew the
images each time they were resized. I'm now converting to bitmaps and I think
I can do much the same thing by drawing to the bitmap's canvas. Thank you for
your help and your great tool MemProof.

If there are any graphics guru's out there, what is the best way to handle
playing cards that are resizeable?

Eric Forbes.

Quote
> I have specifically heard of Windows having resource problems with Metafiles

Other Threads