Skip to content
Permalink
Browse files

Release spatialite context when a spatialite_database_unique_ptr scop…

…es out
  • Loading branch information
m-kuhn committed Nov 15, 2017
1 parent 5c12430 commit 2aed700b16d438f377a834dc6cb64f029e679452
Showing with 33 additions and 24 deletions.
  1. +18 −7 src/core/qgsspatialiteutils.cpp
  2. +15 −17 src/core/qgsspatialiteutils.h
@@ -17,15 +17,23 @@


#include "qgsspatialiteutils.h"
#include "qgslogger.h"

#include <sqlite3.h>
#include <spatialite.h>

int spatialite_database_unique_ptr::open( const QString &path )
spatialite_database_unique_ptr::spatialite_database_unique_ptr()
: std::unique_ptr< sqlite3, std::function<void( sqlite3 * )>> ( nullptr, [this]( sqlite3 * handle )->void
{
deleter( handle );
} )
{
}

int spatialite_database_unique_ptr::open( const QString &path )
{
#if defined(SPATIALITE_HAS_INIT_EX)
void *conn = spatialite_alloc_connection();
mSpatialiteContext = spatialite_alloc_connection();
#else
spatialite_init( 0 );
#endif
@@ -36,7 +44,7 @@ int spatialite_database_unique_ptr::open( const QString &path )

#if defined(SPATIALITE_HAS_INIT_EX)
if ( result == SQLITE_OK )
spatialite_init_ex( database, conn, 0 );
spatialite_init_ex( database, mSpatialiteContext, 0 );
#endif

return result;
@@ -77,12 +85,15 @@ sqlite3_statement_unique_ptr spatialite_database_unique_ptr::prepare( const QStr
return s;
}

void QgsSpatialiteCloser::operator()( sqlite3 *database )
void spatialite_database_unique_ptr::deleter( sqlite3 *handle )
{
sqlite3_close( database );

#if defined(SPATIALITE_HAS_INIT_EX)
spatialite_cleanup_ex( database );
spatialite_cleanup_ex( mSpatialiteContext );
#endif

int res = sqlite3_close( handle );
if ( res != SQLITE_OK )
{
QgsDebugMsg( QString( "sqlite3_close() failed: %1" ).arg( res ) );
}
}
@@ -22,22 +22,7 @@

#include "qgis_core.h"
#include "qgssqliteutils.h"

/**
* \ingroup core
*
* Closes a spatialite database.
*
* \since QGIS 3.0
*/
struct CORE_EXPORT QgsSpatialiteCloser
{

/**
* Closes an spatialite \a database.
*/
void operator()( sqlite3 *database );
};
#include <functional>

/**
* \ingroup core
@@ -47,10 +32,12 @@ struct CORE_EXPORT QgsSpatialiteCloser
*
* \since QGIS 3.0
*/
class CORE_EXPORT spatialite_database_unique_ptr : public std::unique_ptr< sqlite3, QgsSpatialiteCloser>
class CORE_EXPORT spatialite_database_unique_ptr : public std::unique_ptr< sqlite3, std::function<void( sqlite3 * )>>
{
public:

spatialite_database_unique_ptr();

/**
* Opens the database at the specified file \a path.
*
@@ -76,6 +63,17 @@ class CORE_EXPORT spatialite_database_unique_ptr : public std::unique_ptr< sqlit
*/
sqlite3_statement_unique_ptr prepare( const QString &sql, int &resultCode );

private:

/**
* Will be set as deleter for this pointer in the constructor.
*/
void deleter( sqlite3 *handle );

/**
* Keep track of the spatialite context. Set in open(_v2), unset in deleter.
*/
void *mSpatialiteContext = nullptr;
};

#endif // QGSSPATIALITEUTILS_H

0 comments on commit 2aed700

Please sign in to comment.
You can’t perform that action at this time.