sqlite3 c++ forward declaration of struct sqlite3 leads to invalid pointer error on delete in destructor -
using eclipse cdt on linux. here code , warnings during compile:
#ifndef databaseconnection_hpp_ #define databaseconnection_hpp_ #include <sqlite3.h> #include <string> using namespace std; class databaseconnection { private: sqlite3 *_database; public: // constructors databaseconnection(const string &databaseuri, char mode='w'); // destructor ~databaseconnection(); }; #endif /* databaseconnection_hpp_ */
the source
/* * databaseconnection.cpp * * created on: mar 31, 2015 * author: michael wilson (mnw380@gmail.com) */ #include <databaseconnection.hpp> #include <exception.hpp> #include <sqlite3.h> #include <stdlib.h> #include <sstream> #include <formattedstring.hpp> #include <fileutils.hpp> using namespace std; databaseconnection::databaseconnection(const string &databaseuri, char mode) { if (mode != 'w' && mode != 'r') throw exception("exception databaseconnection::databaseconnection. mode must \'r\' or \'w\' read/write connection mode"); int flags = (mode == 'w') ? sqlite_open_readwrite : sqlite_open_readonly; // enables opening databases using uri flags |= sqlite_open_uri; // verify database uri formed correctly if ( !formattedstring::isformatted(databaseuri, "file:/.*") && !formattedstring::isformatted(databaseuri, "http:/.*") ) { // if not formed using standard uri syntax, assume file path , verify exists if ( !fileutils::exists(databaseuri) ) throw exception("exception databaseconnection::databaseconnection. file not exist: " + databaseuri); } // returns non-zero on open error if ( sqlite3_open_v2(databaseuri.c_str(), &_database, flags, null) ) { ostringstream ss; ss << "exception databaseconnection::databaseconnection. error opening database " << databaseuri; throw exception(ss.str()); } } databaseconnection::~databaseconnection() { sqlite3_close(_database); delete _database; }
and warnings during compile
g++ -i/opt/ros/indigo/include -i"/home/user/workspace/project/include" -o0 -g3 -wall -c -fmessage-length=0 -std=gnu++11 -mmd -mp -mf"src/databaseconnection.d" -mt"src/databaseconnection.d" -o "src/databaseconnection.o" "../src/databaseconnection.cpp" ../src/databaseconnection.cpp: in destructor ‘databaseconnection::~databaseconnection()’: ../src/databaseconnection.cpp:48:9: warning: possible problem detected in invocation of delete operator: [enabled default] delete _database; ^ ../src/databaseconnection.cpp:48:9: warning: invalid use of incomplete type ‘struct sqlite3’ [enabled default] in file included /home/user/workspace/project/include/databaseconnection.hpp:11:0, ../src/databaseconnection.cpp:8: /usr/include/sqlite3.h:228:16: warning: forward declaration of ‘struct sqlite3’ [enabled default] typedef struct sqlite3 sqlite3; ^ ../src/databaseconnection.cpp:48:9: note: neither destructor nor class-specific operator delete called, if declared when class defined delete _database;
the approach similar person has done http://www.dreamincode.net/forums/topic/122300-sqlite-in-c/
but want make sure free database pointer 'struct sqlite3' class member
your code attempting issue delete _database;
, _database
has type sqlite3
. problem compiler not know sqlite3
is.
you forward declared sqlite3
is, issue delete
call requires compile know full definition of sqlite3
. did not supply full definition of type.
Comments
Post a Comment