When you create a bitmap in a method, the memory allocated to the bitmap
itself is NOT freed automatically, only the POINTER that you declared in
your VAR clause is freed:

procedure TMyForm.DoSomething;
var
  Bmp : TBitmap; { This is really a pointer to a TBitmap object. It is
created on the stack, and freed on exit. }
begin
  Bmp := TBitmap.Create; { Now the actual TBitmap object gets allocated on
the heap. }
  ...
end; { At this stage Bmp is freed (from the stack), but the TBitmap it
points to (on the heap) is not freed. }

This is why you could then assign Result := Bmp in a function for instance
and pass the bitmap back out.
Similarly, this is why we would use try... finally to ensure the bitmap was
freed.

procedure TMyForm.DoSomething;
var
  Bmp : TBitmap; { This is really a pointer to a TBitmap object. It is
created on the stack, and freed on exit. }
begin
  Bmp := TBitmap.Create; { Now the actual TBitmap object gets allocated on
the heap. }
  try
  ...
  finally
    Bmp.Free; { Frees the memory from the heap }
  end;
end; { At this stage Bmp is freed (from the stack). }

Regards,
  Andrew Cooke.

> -----Original Message-----
> From: Alistair George [SMTP:[EMAIL PROTECTED]]
> Sent: Tuesday, February 16, 1999 6:52 AM
> To:   Multiple recipients of list delphi
> Subject:      [DUG]:  Handles
> 
> Folks after the following code uses the handle of Panel1, I cant get the
> panel back to a useable state - that it was initialised in:
>   hWndC := capCreateCaptureWindowA('My Own Capture Window',
>     WS_CHILD or
>     WS_VISIBLE,
>     0, 0,
>     panel1.Width, panel1.Height,
>     panel1.Handle, 0);
> {now I null hWndC, but I would also like to re-instate the Panel as it was
> intialized}
>   if hWndC <> 0 then begin
>     SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
>     hWndC := 0;
>   end;
> {changing properties of panel1.bringtofront/visible/enabled dont effect.
> Nulling hWndC does not destroy the panel}
>  
> Last question: if I create a bitmap in a subroutine the bitmap is cleared
> on exiting the routine right?? I dont have to bitmap.free before exiting??
> (why I ask is that I see some code written using subproceedures and the
> bitmap is freed prior to exiting.)
>  
> ideas??
> Tks,
> Alistair+
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to