Fundamental "Free" in Delphi

I have one fundamental doubt in Delphi. When I leave a component during development, say, for example, TADOConnectuion, and when I click a button, even I write the following code, I get no errors:

begin ADOConnection.Free; //No error ADOConnection.Free; //No error ADOConnection.Free; //No error end; 

But if I create the same object at runtime, as in the follwos file, I get the message "Access violation ..."

 begin ADOConnection := TADOConnection.create(self); ADOConnection.Free; //No error ADOConnection.Free; //Getting an "Access Violation..." error end; 

I get the same error even if I create an object as shown below:

 ADOConnection := TADOConnection.create(nil); 

I just would like to know the reason for this behavior, that is, why there is no error when I save the component during development?

+6
source share
2 answers

If you release the component, its corresponding field with the owner will be cleared. If you add the ADOConnection development ADOConnection , then

 ADOConnection.Free; // Frees ADOConnection and sets ADOConnection to nil ADOConnection.Free; // Does nothing since ADOConnection is nil 

You can see this by capturing it in a variable:

 var c: TADOConnection; c := ADOConnection; c.Free; // Frees ADOConnection and sets ADOConnection to nil c.Free; // Error: c is not set to nil 

This will not work even when an ADOConnection is created at design time.

Here is an example with the TButton component, which demonstrates how the behavior that you see for development-time components does not apply to development-time components:

 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); published Button: TButton; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin Assert(not Assigned(Button)); TButton.Create(Self).Name := 'Button'; // Button field gets set Assert(Assigned(Button)); Button.Free; // Button field gets cleared Assert(not Assigned(Button)); Button.Free; // Okay, Free may be called on nil values end; end. 
+4
source

ADOConnection is initially zero, so if you free it, a free function will do nothing, because the pointer passed to it is zero. On subsequent calls, the pointer remains zero, so the free one does nothing. When you initialize ADOConnection with create, the pointer contained in ADOConnection is no longer zero, so the first free call will actively free the pointer, but subsequent calls will see that the memory is already freed and throw an exception. The call does not change when calling to free. For this you need freeandnil.

+3
source

Source: https://habr.com/ru/post/924152/


All Articles