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

Alain Baudrez a.baudrez at ...626...
Sat Oct 12 21:42:56 CEST 2013


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



More information about the User mailing list