@@ -189,10 +189,12 @@ exqlite_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
189
189
{
190
190
assert (env );
191
191
192
+ int flags ;
192
193
int rc = 0 ;
193
194
int size = 0 ;
194
- int flags ;
195
195
connection_t * conn = NULL ;
196
+ sqlite3 * db = NULL ;
197
+ ErlNifMutex * mutex = NULL ;
196
198
char filename [MAX_PATHNAME ];
197
199
ERL_NIF_TERM result ;
198
200
@@ -205,28 +207,31 @@ exqlite_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
205
207
return make_error_tuple (env , "invalid_filename" );
206
208
}
207
209
208
- conn = enif_alloc_resource (connection_type , sizeof (connection_t ));
209
- if (!conn ) {
210
- return make_error_tuple (env , "out_of_memory" );
211
- }
212
-
213
210
if (!enif_get_int (env , argv [1 ], & flags )) {
214
211
return make_error_tuple (env , "invalid flags" );
215
212
}
216
213
217
- rc = sqlite3_open_v2 (filename , & conn -> db , flags , NULL );
214
+ rc = sqlite3_open_v2 (filename , & db , flags , NULL );
218
215
if (rc != SQLITE_OK ) {
219
- enif_release_resource (conn );
220
216
return make_error_tuple (env , "database_open_failed" );
221
217
}
222
218
223
- conn -> mutex = enif_mutex_create ("exqlite:connection" );
224
- if (conn -> mutex == NULL ) {
225
- enif_release_resource ( conn );
219
+ mutex = enif_mutex_create ("exqlite:connection" );
220
+ if (mutex == NULL ) {
221
+ sqlite3_close_v2 ( db );
226
222
return make_error_tuple (env , "failed_to_create_mutex" );
227
223
}
228
224
229
- sqlite3_busy_timeout (conn -> db , 2000 );
225
+ sqlite3_busy_timeout (db , 2000 );
226
+
227
+ conn = enif_alloc_resource (connection_type , sizeof (connection_t ));
228
+ if (!conn ) {
229
+ sqlite3_close_v2 (db );
230
+ enif_mutex_destroy (mutex );
231
+ return make_error_tuple (env , "out_of_memory" );
232
+ }
233
+ conn -> db = db ;
234
+ conn -> mutex = mutex ;
230
235
231
236
result = enif_make_resource (env , conn );
232
237
enif_release_resource (conn );
0 commit comments