[Gambas-user] Releasing allocated memory and closing a database

Benoît Minisini gambas at ...1...
Sun Oct 13 13:26:06 CEST 2013


Le 12/10/2013 21:42, Alain Baudrez a écrit :
> In an app where I use a class to retrieve info from a database, I use the
> constructor method _new() to open the connection to the db as follows:
>
> Code snippet:
>
> ' Gambas class file
> ' Name: db.class
>
> Private $db As New Connection
>
> ' Contructor
> Public Sub _new()
> '  Open the connection to the database
>    With $db
>      .Type = "sqlite"
>      .host = Home.User
>      .name = "db.sqlite"
>    End With
>    Try $db.Open
>    IF Error THEN PRINT "Cannot Open Database. Error = "; Error.Text
> End
>
> The question I have is about releasing $db.
>
> Can I assume that the calling SUB or FUNCTION in the main body where I
> instantiated the class 'automagically' closes the database connection and
> releases the allocated memory when the instance to the class is destroyed
> during the termination of the SUB or FUNCTION, or do I have the add a
> destructor in my class like this one:
>
> Code
>
> ' Destructor
> Public Sub _free()
>      $db.Close
> End
>
> ---
> Alain J. Baudrez

The database is automatically closed when the Connection object is freed.

The Connection reference stored in $db is automatically released when 
the Gambas "db" object is freed. If $db is the only reference, then the 
Connection object is freed, and the database is closed.

Regards,

-- 
Benoît Minisini




More information about the User mailing list