diff --git a/src/connection.rs b/src/connection.rs index bee0c40..4e4f176 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -28,12 +28,25 @@ impl Connection { pub fn open_with_flags>(path: T, flags: OpenFlags) -> Result { let mut raw = 0 as *mut _; unsafe { - ok!(ffi::sqlite3_open_v2( + let ret = ffi::sqlite3_open_v2( path_to_cstr!(path.as_ref()).as_ptr(), &mut raw, flags.0, - 0 as *const _, - )); + 0 as *const _ ); + + // Explicitly close the connection on error. + // This is a quirk of the C API, where the database handle + // (raw) is unconditionally allocated. + match ret { + ffi::SQLITE_OK => {}, + code => { + ffi::sqlite3_close(raw); + return Err(::Error { + code: Some(code as isize), + message: None + }); + }, + } } Ok(Connection { raw: raw,