Jump to content
EugeneK

if Obj <> nil then Obj.Free

Recommended Posts

Hi

 

In OverbyteIcsWebSocketCli.pas there are many instances of this

            if Obj <> nil then
                Obj.Free;

I think it should be simplified to just 

                Obj.Free;

 

Share this post


Link to post

Uhmm, I don't remember how FREE procedure is operational in the old versions of Delphi.

 

If the FREE TObject procedure doesn't test the NIL before destroy it ...

 

So for new Delphi version is OK, but for older you should see.

 

  • Like 1

Share this post


Link to post
1 hour ago, Angus Robertson said:

ICS still supports Delphi 7. 

 

Angus

 

Free on nil works in Delphi 7 as well.

  • Like 1

Share this post


Link to post
1 hour ago, DelphiUdIT said:

Uhmm, I don't remember how FREE procedure is operational in the old versions of Delphi.

 

If the FREE TObject procedure doesn't test the NIL before destroy it ...

 

So for new Delphi version is OK, but for older you should see.

 

If you have license Delphi 7 is available for download on Embarcadero website, you can check source code there.

Share this post


Link to post
2 hours ago, DelphiUdIT said:

Uhmm, I don't remember how FREE procedure is operational in the old versions of Delphi.

Delphi 1

; procedure TObject.Free

ObjectFree:

	MOV	BX,SP
	LES	DI,SS:[BX+4]
	MOV	AX,ES
	OR	AX,DI
	JE	@@1
	MOV	AL,1
	PUSH	AX
	PUSH	ES
	PUSH	DI
	LES	DI,ES:[DI]
	CALL	ES:[DI].vtDestroy
@@1:	RETF	4

Delphi 2

procedure TObject.Free;
asm
        TEST    EAX,EAX
        JE      @@exit
        MOV     ECX,[EAX]
        MOV     DL,1
        CALL    dword ptr [ECX].vtDestroy
@@exit:
end;

 

  • Like 3

Share this post


Link to post

@Eugene Kryukov

My only concern was related to the scrupulousness of maintaining the compatibility with the past that ICS still maintains. I don't need proof or anything else.

 

@Anders Melander

I was referring to old versions, not ancient versions :classic_biggrin:

(I know you meant to point out that the nil test has existed since the first versions of Delphi)

Share this post


Link to post

Lines from file "c:\Program Files (x86)\Borland\Delphi7\Source\Rtl\Sys\System.pas" 

procedure TObject.Free;
begin
  if Self <> nil then
    Destroy;
end;

 

In theory, all this can be replaced with a more secure function FreeAndNil(Obj).

 

Share this post


Link to post

In general, ICS does use FreeAndNil for new code, if not nilled in code, the nil being the important part, since double Free without nil does cause an exception.  Double free is common, due to the complex inheritance of many ICS components. 

 

But I simply don't have the time to clean up code written up to 25 years ago, unless I'm updating that code for other reasons.  It all needs testing afterwards, and often correction when   it then fails to compile on older versions of Delphi...

 

Angus

 

  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×