Skip to content

Commit

Permalink
qgsstyle.cpp: avoid minor memleaks in use of sqlite3_mprintf()
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jun 3, 2018
1 parent 79ba0ee commit 8aeb2f6
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 122 deletions.
3 changes: 1 addition & 2 deletions python/core/auto_generated/symbology/qgsstyle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,11 @@ Is emitted every time a tag or smartgroup has been added, removed, or renamed
Convenience function to open the DB and return a sqlite3 object
%End

bool runEmptyQuery( char *query, bool freeQuery = true );
bool runEmptyQuery( const QString &query );
%Docstring
Convenience function that would run queries which don't generate return values

:param query: query to run
:param freeQuery: release query memory

:return: success true on success
%End
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgssqliteutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgssqliteutils.h"

#include <sqlite3.h>
#include <stdarg.h>

void QgsSqlite3Closer::operator()( sqlite3 *database )
{
Expand Down Expand Up @@ -89,3 +90,14 @@ sqlite3_statement_unique_ptr sqlite3_database_unique_ptr::prepare( const QString
s.reset( preparedStatement );
return s;
}

QString QgsSqlite3Mprintf( const char *format, ... )
{
va_list ap;
va_start( ap, format );
char *c_str = sqlite3_vmprintf( format, ap );
va_end( ap );
QString res( QString::fromUtf8( c_str ) );
sqlite3_free( c_str );
return res;
}
10 changes: 9 additions & 1 deletion src/core/qgssqliteutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,15 @@ class CORE_EXPORT sqlite3_database_unique_ptr : public std::unique_ptr< sqlite3,
* argument will be filled with the sqlite3 result code.
*/
sqlite3_statement_unique_ptr prepare( const QString &sql, int &resultCode ) const;

};


/**
* Wraps sqlite3_mprintf() by automatically freeing the memory.
* \note not available in Python bindings.
* \since QGIS 3.2
*/
QString CORE_EXPORT QgsSqlite3Mprintf( const char *format, ... );


#endif // QGSSQLITEUTILS_H
Loading

0 comments on commit 8aeb2f6

Please sign in to comment.