Board index » delphi » TBitmap freed twice caused an Access Violation.

TBitmap freed twice caused an Access Violation.

I have noticed a few times that if a TBitmap has already been freed, the
second call to free causes an Access Violation.

I'm using Delphi 4.  My understanding of the Free method was that it
would never cause access violations, but now this doesn't seem to be the
case.  Is this a bug, or am I doing something wrong here?

Thanks,
Dennis

 

Re:TBitmap freed twice caused an Access Violation.


No bug,  once the Tbitmap is free the first time the code entry no longer
exist! calling
free again will send you to some never land..
 if your having problems trying to keep track if the image is free or not,
then at app start up in your
form create set the Bitmap image to nil...
then each time you want to free this image you first check it for nil or use
assign ?
for example
 if Assigned(MyBitMap) then
   beign
      MyBitMap.Free;
      MyBitMap := Nil;
   end;
Quote
Dennis Palmer wrote:
> I have noticed a few times that if a TBitmap has already been freed, the
> second call to free causes an Access Violation.

> I'm using Delphi 4.  My understanding of the Free method was that it
> would never cause access violations, but now this doesn't seem to be the
> case.  Is this a bug, or am I doing something wrong here?

> Thanks,
> Dennis

Re:TBitmap freed twice caused an Access Violation.


Quote
On Thu, 16 Sep 1999 23:46:34 -0700, Dennis Palmer <104446.2...@compuserve.com> wrote:
>I have noticed a few times that if a TBitmap has already been freed, the
>second call to free causes an Access Violation.

>I'm using Delphi 4.  My understanding of the Free method was that it
>would never cause access violations, but now this doesn't seem to be the
>case.  Is this a bug, or am I doing something wrong here?

It's like this with all objects. Sine the first free will basically remove the object
from memory, the second call will just jump to a place where it used to be an
object, but now doesnt contain anything meaningful, and will generate an AV
or similar errors

if you have a common variable, set it to nil after freeing it, that way you can
test it

if assigned(MyObj) then
  MyObj.Free
  MyObj:= nil

- Asbj?rn

Re:TBitmap freed twice caused an Access Violation.


Quote
>I have noticed a few times that if a TBitmap has already been freed, the
>second call to free causes an Access Violation.

>I'm using Delphi 4.  My understanding of the Free method was that it
>would never cause access violations, but now this doesn't seem to be the

Your understanding of this is not quite on target. Free has always been safe
to use on a nil varaible. Internally Free tests for nil before calling
Destroy or whatever. If you always set your variable to nil after Free'ing
you will be safe. No change here.

Jon

Re:TBitmap freed twice caused an Access Violation.


Quote
On Fri, 17 Sep 1999 05:38:16 -0500, "Jon Jacobs" <jqjac...@gte.net> wrote:
>>I have noticed a few times that if a TBitmap has already been freed, the
>>second call to free causes an Access Violation.

>>I'm using Delphi 4.  My understanding of the Free method was that it
>>would never cause access violations, but now this doesn't seem to be the

>Your understanding of this is not quite on target. Free has always been safe
>to use on a nil varaible. Internally Free tests for nil before calling
>Destroy or whatever. If you always set your variable to nil after Free'ing
>you will be safe. No change here.

I stand corrected,
however, if you share the object, it's still a problem of freeing twice

- Asbj?rn

Re:TBitmap freed twice caused an Access Violation.


Quote
Lord Crc wrote:

> On Fri, 17 Sep 1999 05:38:16 -0500, "Jon Jacobs" <jqjac...@gte.net> wrote:

> >>I have noticed a few times that if a TBitmap has already been freed, the
> >>second call to free causes an Access Violation.

> >>I'm using Delphi 4.  My understanding of the Free method was that it
> >>would never cause access violations, but now this doesn't seem to be the

> >Your understanding of this is not quite on target. Free has always been safe
> >to use on a nil varaible. Internally Free tests for nil before calling
> >Destroy or whatever. If you always set your variable to nil after Free'ing
> >you will be safe. No change here.

> I stand corrected,
> however, if you share the object, it's still a problem of freeing twice

> - Asbj?rn

I'm learning everyday :-)
(though of course I should have known this)
Here's a question about the other end: is there a way to check if a bitmap (or
other object) is created?

Thank you,

Remco
http://www.xs4all.nl/~remcodek/vic.html

Re:TBitmap freed twice caused an Access Violation.


On Sat, 18 Sep 1999 04:12:34 +0200, Remco de Korte

Quote
<remco...@xs4all.nl> wrote:
>Here's a question about the other end: is there a way to check if a bitmap (or
>other object) is created?

Not reliably. You should take care of that kind of information in the
logic of your code. In other words, your code should never find itself
in a position where it does not know whether or not an object has been
instantiated. It should either know intrinsically, by virtue of the
logical flow, or else it should explicitly keep track via a boolean
flag or some similar means.

-Steve

Re:TBitmap freed twice caused an Access Violation.


Quote
Steve Schafer (TeamB) wrote:

> On Sat, 18 Sep 1999 04:12:34 +0200, Remco de Korte
> <remco...@xs4all.nl> wrote:

> >Here's a question about the other end: is there a way to check if a bitmap (or
> >other object) is created?

> Not reliably. You should take care of that kind of information in the
> logic of your code. In other words, your code should never find itself
> in a position where it does not know whether or not an object has been
> instantiated. It should either know intrinsically, by virtue of the
> logical flow, or else it should explicitly keep track via a boolean
> flag or some similar means.

> -Steve

OK, that confirms what I thought, but it is good to hear it from reliable source
8)

Thanks,

Remco

Re:TBitmap freed twice caused an Access Violation.


On Sat, 18 Sep 1999 13:22:18 +0200, Remco de Korte

Quote
<remco...@xs4all.nl> wrote:
>it is good to hear it from reliable source

What does that have to do with me? :)

-Steve

Re:TBitmap freed twice caused an Access Violation.


Quote
Steve Schafer (TeamB) wrote:

> On Sat, 18 Sep 1999 13:22:18 +0200, Remco de Korte
> <remco...@xs4all.nl> wrote:

> >it is good to hear it from reliable source

> What does that have to do with me? :)

> -Steve

Oops, sorry, my mistake! Mummy always warned me not to trust strange men... 8}

Other Threads