[Gambas-user] Memory Management

Benoit Minisini gambas at ...1...
Sat Sep 8 10:19:21 CEST 2007


On jeudi 06 septembre 2007, Vince Scott wrote:
>   Hi All,
>   I would like to know how Gambas handles memory cleanup. What is the
> proper way in Gambas for the developer to practice good memory management?
> In Visual Basic objects created within a function or sub-routine are
> automatically removed after the method is exited. Objects created in public
> space must be removed by the code. Is this the same for Gambas? What are a
> good set of basic rules to use in Gambas to ensure you have no memory
> leaks? How can you check and validate this? Any information on the subject
> appreciated.
>   Thanks,
>   Vince Scott

In Gambas, objects life is managed by reference counting:

* When an object is created, its reference counter is zero.

* Each time you put a reference on an object inside any variable, its 
reference counter is incremented.

* Each time this reference is removed by assignation to NULL or another 
reference, the reference counter is decremented.

* When an object reference counter becomes null again, the object is freed.

So that means memory management is automatic, unless you create circular 
references: object A has a variable having a reference on B, and object B has 
a variable having a reference on A. 

Gambas is unable to detect the cycle and cannot free the two objects. 

You must do that by hand then, breaking the cycle by releasing one of the two 
references yourself.

Otherwise, at program ends, you have the famous message "Circular reference 
detected" with the list of each reference of each class being not freed.

Note that what you describe for VB is not really accurate. AFAIK, it works 
like Gambas, by reference counting.

Regards,

-- 
Benoit Minisini




More information about the User mailing list