[Gambas-user] Fwd: _free not called when object destroyed

Demosthenes Koptsis demosthenesk at gmail.com
Wed Aug 2 21:25:54 CEST 2023


i attache the project



-------- Forwarded Message --------
Subject: 	Re: [Gambas-user] _free not called when object destroyed
Date: 	Wed, 2 Aug 2023 22:22:47 +0300
From: 	Demosthenes Koptsis <demosthenesk at gmail.com>
To: 	user at lists.gambas-basic.org



For example, suppose you have a class Fruit.class and Main.module of a 
command line project

------- Fruit.class ------------

' Gambas class file

Public Name As String

Static Public Sub _init()
   Print "Fruit is created, i am in _init"
End

Public Sub _free()
   Print "Fruit is freed, i am in _free"
End
--------------------------------------------------

---------- Main.module --------------------

' Gambas module file

Public Sub Main()

   'call CreateFruit
   Print "I am in Main"
   CreateFruit()
   Print "I am back in Main after CreateFruit"

End

Public Sub CreateFruit()
    Dim oApple As New Fruit
    oApple.Name = "Apple"
    Print "I am in CreateFruit, oApple Name is: " & oApple.Name

    Print "I am in CreateFruit, before Apple=Null"
    oApple = Null
    Print "I am in CreateFruit, after oApple=Null"
End
---------------------------------------

You can create a Fruit object in a CreateFruit sub but when the code 
goes out of scope of CreateFruit Sub the object is destroyed or if you 
set its reference variable to NULL as it shows in Console

----------- Console -------------------

I am in Main
Fruit is created, i am in _init
I am in CreateFruit, oApple Name is: Apple
I am in CreateFruit, before Apple=Null
*Fruit is freed, i am in _free**
*I am in CreateFruit, after oApple=Null
I am back in Main after CreateFruit

------------------------------------------

if you set oApple = Null the object is freed



On 8/2/23 19:32, Brian G wrote:
> I have a question regarding when an object is destroyed and _free is 
> called.
>
> in my code when I do the following then the object free method is called
>
> dim a as myobject = new myobject
>
> a = new myobject     ' when I do this then the old object _free method 
> is called
>
> But if I
>
> a = NULL                  ' The _free method is never called for the 
> object or for any object in an array of objects
>
> Maybe I don't understand correctly how this works, but I would expect 
> the objects _free method to be called if there is no other reference.
>
>
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230802/9be14b93/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DestroyObject.tar.gz
Type: application/gzip
Size: 12921 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230802/9be14b93/attachment-0001.gz>


More information about the User mailing list