Autore Topic: Rilasciare memoria allocata e chiudere un database  (Letto 295 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.311
  • Ne mors quidem nos iunget
    • Mostra profilo
Rilasciare memoria allocata e chiudere un database
« il: 13 Ottobre 2013, 14:44:24 »
Vi riporto questa discussione apparsa nella M.L. ufficiale:


" 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:

Codice: gambas [Seleziona]

' 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

Codice: gambas [Seleziona]

' 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
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »