Where should the class free its private variables?

As there is no garbage collection in Delphi, where exactly do you unload the variables?

Let's say I have a type with a set of private vars. Is it enough to have a Destroy method that will work? Should I explicitly call this destroy method in my consumption classes?

+3
source share
4 answers

The best way to organize destruction in a dolphin is to always think about "who will create these vars."

If you also free them in this context (private methods for deleting a class for you) it is much less likely that you will encounter memory leaks.

In fact, a class destructor is not usually called through

myInstance.Destroy();

FreeAndNil(myInstance);

myInstance.Free();

delphi

+10

, : .Free( .Destroy) FreeAndNil.

: , , , . .

, , , , . , , , , .

: TLight TTimer, , . TLight. TLight TControl TComponent, Timer Self Owner, , TLight .

+4

:

(, , , , ), , , : , .

(, , ): , .

, : TComponent, , TComponent, :

  • TInterfacesObject : (. )
  • : Object.Free FreeAndNil (Object), ( ).

, ( TComponent , ): .

OP:

. TTimer.Create(), TComponent. , ? ()?

, TComponent can, (, ). , !! TTimer.Create(), TComponent, - TForm ( , !).

- , .

+3

" vars" " ". (, , ), , , , " ". ( - Java .NET, no; -).

, , . 4 , , .

unit Foo;
interface
  // put things here you want other units to see
implementation
  // put the *implementation* of the things from the interface here along with things
  // you only want this unit to see.
initialization
  // <optional> put code here that you want to execute when this unit is "loaded"
finalization
  // <optional> put code here that you want to execute when this unit is "unloaded"
end.

, , ( ), .

+1
source

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


All Articles