title: sqlite3 c++ forward declaration of struct sqlite3 leads to invalid pointer error on delete in destructor
body: <p>Using Eclipse CDT on Linux. Here is the code and the warnings during compile:</p>
  
  <pre><code> #ifndef DATABASECONNECTION_HPP_
  #define DATABASECONNECTION_HPP_
  
  #include &lt;sqlite3.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class DatabaseConnection
  {
  private:
      sqlite3 *_database;
  public:
      // constructors
      DatabaseConnection(const string &amp;databaseURI, char mode='w');
  
      // destructor
      ~DatabaseConnection();
  };
  
  #endif /* DATABASECONNECTION_HPP_ */
  </code></pre>
  
  <p>the source</p>
  
  <pre><code>/*
   * DatabaseConnection.cpp
   *
   *  Created on: Mar 31, 2015
   *      Author: Michael Wilson (mnw380@gmail.com)
   */
  
  #include &lt;DatabaseConnection.hpp&gt;
  #include &lt;Exception.hpp&gt;
  #include &lt;sqlite3.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sstream&gt;
  #include &lt;FormattedString.hpp&gt;
  #include &lt;FileUtils.hpp&gt;
  
  using namespace std;
  
  DatabaseConnection::DatabaseConnection(const string &amp;databaseURI, char mode)
  {
  
     if (mode != 'w' &amp;&amp; mode != 'r')
          throw Exception("Exception DatabaseConnection::DatabaseConnection. Mode must be \'r\' or \'w\' for read/write connection mode");
  
      int flags = (mode == 'w') ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
  
      // this enables opening databases using URI
      flags |= SQLITE_OPEN_URI;
  
      // verify the database URI is formed correctly
      if ( !FormattedString::isFormatted(databaseURI, "file:/.*") &amp;&amp; !FormattedString::isFormatted(databaseURI, "http:/.*") ) {
          // if not formed using standard URI syntax, then assume a file path and verify it exists
          if ( !FileUtils::Exists(databaseURI) )
              throw Exception("Exception DatabaseConnection::DatabaseConnection. File does not exist: " + databaseURI);
      }
  
  
      // returns non-zero on open error
      if ( sqlite3_open_v2(databaseURI.c_str(), &amp;_database, flags, NULL) ) {
          ostringstream ss;
          ss &lt;&lt; "Exception DatabaseConnection::DatabaseConnection. Error opening database " &lt;&lt; databaseURI;
          throw Exception(ss.str());
      }
  }
  
  DatabaseConnection::~DatabaseConnection()
  {
      sqlite3_close(_database);
      delete _database;
  }
  </code></pre>
  
  <p>and the warnings during compile</p>
  
  <pre><code>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 by default]
    delete _database;
           ^
  ../src/DatabaseConnection.cpp:48:9: warning: invalid use of incomplete type ‘struct sqlite3’ [enabled by default]
  In file included from /home/user/workspace/Project/include/DatabaseConnection.hpp:11:0,
                   from ../src/DatabaseConnection.cpp:8:
  /usr/include/sqlite3.h:228:16: warning: forward declaration of ‘struct sqlite3’ [enabled by default]
   typedef struct sqlite3 sqlite3;
                  ^
  ../src/DatabaseConnection.cpp:48:9: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
    delete _database;
  </code></pre>
  
  <p>The approach is very similar to what this person has done <a href="http://www.dreamincode.net/forums/topic/122300-sqlite-in-c/" rel="nofollow">http://www.dreamincode.net/forums/topic/122300-sqlite-in-c/</a></p>
  
  <p>But I want to make sure to free the database pointer 'struct sqlite3' class member</p>