Recent

Author Topic: [solved] TBitmap free or destroy  (Read 12984 times)

dcelso

  • Full Member
  • ***
  • Posts: 158
[solved] TBitmap free or destroy
« on: January 30, 2013, 05:17:11 pm »
¿Hello, anyone knows the difference bettween free a tbitmat with .free o .destroy?
« Last Edit: February 01, 2013, 04:56:05 pm by dcelso »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TBitmap free or destroy
« Reply #1 on: January 30, 2013, 05:32:16 pm »
Press Alt+Up and you will see:
Code: [Select]
procedure TObject.Free;

        begin
           // the call via self avoids a warning
           if self<>nil then
             self.destroy;
        end;
             
So you see that Free calls Destroy.

And even better than "BMP.Free;" can be:
Code: [Select]
FreeThenNil(BMP);
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: TBitmap free or destroy
« Reply #2 on: January 30, 2013, 05:37:56 pm »
 :o, thanks. I'll remember from now.

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: TBitmap free or destroy
« Reply #3 on: January 30, 2013, 05:42:23 pm »
Then, if I need do it in a loop (of minimum 1000 times), and I control well the non null access,
is it better use .destroy to gain performance and do faster the application.?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TBitmap free or destroy
« Reply #4 on: January 30, 2013, 06:52:47 pm »
you should not create and destroy bitmaps in a loop you can clear and reuse them as needed it ill be faster mainly from avoiding to re create all the internal structures and memory allocation and re allocation etc.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TBitmap free or destroy
« Reply #5 on: January 30, 2013, 07:37:19 pm »
And even better than "BMP.Free;" can be:
Code: [Select]
FreeThenNil(BMP);
I have seen this typo one or more times before. FreeAndNil(object) is correct form.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TBitmap free or destroy
« Reply #6 on: January 30, 2013, 07:44:19 pm »
It's not typo, they exist both.
If you have enough time: http://www.mail-archive.com/lazarus@lists.lazarus.freepascal.org/msg19483.html
There are maybe 100 mails but honestly, I did not fully catch the difference  ::)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: TBitmap free or destroy
« Reply #7 on: January 31, 2013, 06:16:50 pm »
I not find FreeThenNil fuction unit in lazarus 1.1 (daily). Only I found FreeAndNil on sysutilh.inc

Is This function in another unit?


User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TBitmap free or destroy
« Reply #8 on: January 31, 2013, 07:12:54 pm »
It was in LCLProc unit, so it's not found in Delphi either.

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: TBitmap free or destroy
« Reply #9 on: February 01, 2013, 08:53:42 am »
 :o, i saw it.
Code: [Select]
procedure FreeThenNil(var obj);
begin
  if Pointer(obj) <> nil then
  begin
    TObject(obj).Free;
    Pointer(obj) := nil;
  end;
end;

 procedure FreeAndNil(var obj);
      var
        temp: tobject;
      begin
        temp:=tobject(obj);
        pointer(obj):=nil;
        temp.free;
 end;
But I don't see the difference, both do the same for me.
Anyone can explain me the real difference in a application, for example, like me, a loop.
Thanks

Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: TBitmap free or destroy
« Reply #10 on: February 01, 2013, 09:21:24 am »
FreeAndNil will crash if object destructor accesses Self.
FreeThenNil is not thread-safe, since there is a window for a race between destroying an object and clearing the reference.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TBitmap free or destroy
« Reply #11 on: February 01, 2013, 09:38:06 am »
It's not typo, they exist both.
If you have enough time: http://www.mail-archive.com/lazarus@lists.lazarus.freepascal.org/msg19483.html
There are maybe 100 mails but honestly, I did not fully catch the difference  ::)

for a small example of the differences on usage take a look at  http://www.lazarus.freepascal.org/index.php/topic,19207.msg109134.html#msg109134 this should give you an idea about the problems that might raise. Let me also add at this point that using freeandnil makes extending components a bit easier avoiding some of the flow problems that might be introduced on a descendant component from, usually, a different developer, who might or might not have the time to properly analyze the underline code.

FreeAndNil will crash if object destructor accesses Self.

I'd like some example of this, as far as I can understand freeandnil should set the variable holding the reference to the object to nil not the self "variable" of the object. If it does play havoc with the self "variable" then there is something wrong that needs investigation.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TBitmap free or destroy
« Reply #12 on: February 01, 2013, 09:38:28 am »
A destructor that accesses Self is poorly written, and normally ought to crash. So you are safer sticking to FreeAndNil (unless you really know what you are doing, or imagine you do).

Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: TBitmap free or destroy
« Reply #13 on: February 01, 2013, 10:06:10 am »
Quote
freeandnil should set the variable holding the reference to the object to nil not the self "variable" of the object.
Yes, I wrote too hastily. I meant "if object destructor accesses Self indirectly".
For example, consider TForm.Destroy which calls "OnDestroy" event handler,
which might be written by another programmer and carelessly access "Form1.SomeProperty".
(Actual TForm.Destroy does not work so simply, it is just an example).

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TBitmap free or destroy
« Reply #14 on: February 01, 2013, 10:29:41 am »
thank you for the clarification and I agree.
Yes, I wrote too hastily. I meant "if object destructor accesses Self indirectly".
For example, consider TForm.Destroy which calls "OnDestroy" event handler,
which might be written by another programmer and carelessly access "Form1.SomeProperty".

thank you for the clarification.

(Actual TForm.Destroy does not work so simply, it is just an example).


It does not matter how complicated the logic behind the destruction process is the variable has been set to nil before the destructor is called so it should raise an exception.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018