How to free all memory allocated by an object - Delphi

Asked

Viewed 9,879 times

10

What is the best way to free all memory allocated by creating an object for S.O.

Let’s not consider:

Objeto.Free;
Objeto := Nil;
Sysutils.FreeAndNil(Objeto);

There would be more ways to free the memory for the S.O.?

  • 4

    Is your question merely educational? Why can’t we consider the methods mentioned?

  • 2

    use only Objeto := Nil; will not be freeing the memory, only makes the variable does not point more to the object, the object will still be in memory and to make it worse you no longer have a pointer to it, so it will be impossible to free the memory occupied by this object, This is what we call memory leakage

  • This question lacks explanation. The author excluded possible answers in the question itself but did not give the motivation. It does not thus show application of the answer.

3 answers

6

Taking advantage of Math’s answer, if vc simply set nil to the object it is still allocated, but vc is disassociating the variable pointer from the memory area occupied by the object.

When you call the method

Objeto.Free

you are making the release of this object from memory

the difference between the methods . Free and Freeandnil is that free and nil after releasing the object arrow the nil value for the variable.

Using Freeandnil is more common to avoid errors where you after releasing an object want to validate if the object has information:

Objeto := TObject.Create;
Objeto.Free;
if Assigned(Objeto) then
  ShowMessage('Vai entrar aqui.');

In another case:

Objeto := TObject.Create;
FreeAndNil(Objeto);
if Assigned(Objeto) then
  ShowMessage('NÃO vai entrar aqui.');

thus the answer to your question, both the . Free when Freeandnil releases the whole object and the question between using Free and Freeandnil is based more on good practices

  • 2

    But the question is about other methods to do it. He says "Would plus ways to release the memory(...)?"

  • @Yes, but as the question of the title was How to free all memory allocated by an object and the fact that Objeto := nil does not release memory (except in case of interfaces with reference count reaching 0) thought q fit the explanation.

  • But it is still not an answer to the question. So it should have been posted as a comment, for example. Its explanation is excellent. Only it’s not an answer. :(

2

You can use the following function that frees the memory and Zera the object, which would be the following function:

procedure FreeMemAndNil(var ptr; size: Integer = -1);
var
  p: Pointer;
begin
  p := Pointer(ptr);
  if p <> nil then
  begin
    if size > -1 then
      FreeMem(p, size)
    else
      FreeMem(p);
    Pointer(ptr) := nil;
  end;
end;

Or simply use the function FreeMem(pointer,size);

Reference

  • If Freemem destroys the object, can you instantiate an object with Allocmem? Allocmem can be used to instantiate another reference of a record type, for example, what is the difference between all these cases?

2

The language documentation explains that the correct way to free the memory occupied by a class instance is the invocation of the destructor of this class.

However, for the sake of good practice, the methods should always be used Free or FreeAndNil (as already explained in other answers) and not invoke the method Destroy directly, although this is indeed possible.

After the execution of the destructor, the method FreeInstance is invoked for the Delphi memory manager to return this memory block which was occupied by the instance.

However, this does not mean that the memory will be returned to the operating system, generally the same will be available in a chained list of free memory blocks to be used again without having to re-allocate system memory (costly process).

The memory block is only actually released to the operating system when it corresponds to a memory block from the end of the heap (dynamic allocation memory area). In this case the memory manager will search shrink the heap, releasing all adjoining blocks from the end to the beginning.

The process is this why it is not possible to release memory blocks from the middle of the heap, leaving gaps in it. The heap must be contiguous.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.