@@ -37,6 +37,7 @@ class LocalDbBridge extends LocalSbRequestImpl {
3737 LocalDbResult <DynamicLibrary , String >? _lib;
3838 Pointer <AppDbState >? _dbInstance; // Cambiado de late a nullable
3939 String ? _lastDatabaseName; // Almacena el último nombre de base de datos utilizado
40+ bool _hotRestartDetected = false ; // Flag para detectar hot restart
4041
4142 Future <void > initForTesting (String databaseName, String libPath) async {
4243 if (! databaseName.contains ('.db' )) {
@@ -91,18 +92,23 @@ class LocalDbBridge extends LocalSbRequestImpl {
9192
9293 /// Método para verificar si la conexión es válida y reinicializar si es necesario
9394 Future <bool > ensureConnectionValid () async {
94- // Verificar si la instancia es válida
95- if (_dbInstance == null || _dbInstance == nullptr) {
96- log ('Database connection invalid, attempting to reinitialize...' );
95+ // Verificar si la instancia es válida o si se detectó hot restart
96+ if (_dbInstance == null || _dbInstance == nullptr || _hotRestartDetected ) {
97+ log ('Database connection invalid (hot restart: $ _hotRestartDetected ) , attempting to reinitialize...' );
9798
9899 if (_lastDatabaseName != null ) {
99100 try {
101+ // Reset hot restart flag
102+ _hotRestartDetected = false ;
103+
104+ // Reinicializar solo la instancia de base de datos, no la librería
100105 final appDir = await getApplicationDocumentsDirectory ();
101106 await _init ('${appDir .path }/$_lastDatabaseName ' );
102- log ('Database reinitialized successfully' );
107+ log ('Database reinitialized successfully after hot restart ' );
103108 return true ;
104109 } catch (e) {
105110 log ('Failed to reinitialize database: $e ' );
111+ _hotRestartDetected = true ; // Marcar para próximo intento
106112 return false ;
107113 }
108114 } else {
@@ -111,6 +117,25 @@ class LocalDbBridge extends LocalSbRequestImpl {
111117 }
112118 }
113119
120+ // Test the connection with a simple operation to detect stale pointers
121+ try {
122+ if (_dbInstance != null && _dbInstance != nullptr) {
123+ // Intentar una operación mínima para verificar si el puntero es válido
124+ final testResult = _get (_dbInstance! );
125+ if (testResult == nullptr) {
126+ log ('Database pointer appears stale, marking for reinitialization' );
127+ _hotRestartDetected = true ;
128+ return await ensureConnectionValid (); // Recursiva para reinicializar
129+ }
130+ // Liberar el resultado de test inmediatamente
131+ malloc.free (testResult);
132+ }
133+ } catch (e) {
134+ log ('Connection test failed, marking for reinitialization: $e ' );
135+ _hotRestartDetected = true ;
136+ return await ensureConnectionValid (); // Recursiva para reinicializar
137+ }
138+
114139 return true ;
115140 }
116141
@@ -161,10 +186,16 @@ class LocalDbBridge extends LocalSbRequestImpl {
161186 throw Exception ('Failed to create database instance. Returned null pointer.' );
162187 }
163188
189+ // Reset hot restart flag on successful initialization
190+ _hotRestartDetected = false ;
191+ log ('Database instance created successfully: ${_dbInstance .toString ()}' );
192+
164193 calloc.free (dbNamePointer);
165194 } catch (error, stackTrace) {
166195 log ('Error in _init: $error ' );
167196 log (stackTrace.toString ());
197+ _hotRestartDetected = true ;
198+ rethrow ;
168199 }
169200 }
170201
0 commit comments