title: MySQL C++ Connector crashes my app at ResultSet->getString()
body: <p>It's me again probably asking noob C++ questions</p>
  
  <p>I had MAJOR headaches making the darn (sorry for the language) MySQL C++ connector work. I don't know if it is poorly written or something, but for my experience yet I've never had so much trouble making something to work. </p>
  
  <p>Anyhow I got it to connect and throw exceptions on failed connect/query which for me is quite big thing :U :P . The actual problem comes out of me obtaining the result of the query. Regardless of what I do my application always crashes :S </p>
  
  <p>I used the 32-bit installer and the libmysql.dll/lib from the 32-bit MySQL server (since i'm compiling a 32-bit application i figured this is the right thing to do)</p>
  
  <p>Here's some code so you could imagine what I'm talking about</p>
  
  <p><strong>DBManager.h</strong></p>
  
  <pre><code>#ifndef DBMANAGER_H
  #define DBMANAGER_H
  #define CPPCONN_PUBLIC_FUNC
  #define CPPCONN_LIB_BUILD True
  
  #include &lt;string&gt;
  #include "mysql_connection.h"
  #include "mysql_driver.h"
  #include &lt;cppconn/driver.h&gt;
  #include &lt;cppconn/exception.h&gt;
  #include &lt;cppconn/resultset.h&gt;
  #include &lt;cppconn/statement.h&gt;
  #include &lt;cppconn/prepared_statement.h&gt;
  
  class DBManager
  {
  public:
      static DBManager* Instance();
      bool Query(const char* Query);
      void Connect(const char* DbHost, unsigned short DbPort, const char* DbUser, const char* DbPass, const char* DbName);
      bool ValidCredentials(const char* Username, const char* Password);
      void ManageException(sql::SQLException &amp;e);
  
      ~DBManager();
  
  protected:
      static DBManager* pInstance;
  
  private:
      DBManager() {};
      DBManager(DBManager const&amp;){};
      DBManager&amp; operator=(DBManager const&amp;){};
  
      sql::mysql::MySQL_Driver* driver;
      sql::Connection *Con;
      sql::PreparedStatement *pstmt;
      sql::ResultSet *res;
      sql::Statement *stmt;
  
      bool isConnected;
  };
  
  #endif
  </code></pre>
  
  <p>And now the cpp file <strong>DBManager.cpp</strong></p>
  
  <pre><code>#include "DBManager.h"
  
  DBManager* DBManager::pInstance = NULL;
  
  DBManager* DBManager::Instance()
  {
      if (!pInstance)
      {
          pInstance = new DBManager();
      }
  
      return pInstance;
  }
  
  bool DBManager::Query(const char* Query)
  {
      return true;
  }
  
  DBManager::~DBManager()
  {   
      delete Con;
      delete pstmt;
      delete res;
      delete stmt;
  }
  
  void DBManager::ManageException(sql::SQLException&amp; e)
  {
      if (e.getErrorCode() != 0) {
          std::cout &lt;&lt; "# ERR: SQLException in " &lt;&lt; __FILE__;
          std::cout &lt;&lt; "(" &lt;&lt; __FUNCTION__ &lt;&lt; ") on line " &lt;&lt; __LINE__ &lt;&lt; std::endl;
          std::cout &lt;&lt; "# ERR: " &lt;&lt; e.what();
          std::cout &lt;&lt; " (MySQL error code: " &lt;&lt; e.getErrorCode();
          std::cout &lt;&lt; ", SQLState: " &lt;&lt; e.getSQLState() &lt;&lt; " )" &lt;&lt; std::endl;
      }
  }
  
  void DBManager::Connect(const char* DbHost, unsigned short DbPort, const char* DbUser, const char* DbPass, const char* DbName)
  {
      try {
          driver = sql::mysql::get_mysql_driver_instance();
          std::string connDSN = "tcp://" + std::string(DbHost) + ":3306";
  
          Con = driver-&gt;connect(connDSN, sql::SQLString(DbUser), sql::SQLString(DbPass));
          Con-&gt;setSchema(sql::SQLString(DbName));
          isConnected = true;
  
          std::cout&lt;&lt;"Database connection successul."&lt;&lt;std::endl;
  
      } catch(sql::SQLException &amp;e) {
          ManageException(e);
          isConnected = false;
  
          return;
      }
  }
  
  bool DBManager::ValidCredentials(const char* Username, const char* Password)
  {
      bool cred = false;
  
      try {
          pstmt = Con-&gt;prepareStatement("SELECT * FROM account WHERE account_name=? LIMIT 1"); // Smart use of indexing
          pstmt-&gt;setString(1, Username);
          res = pstmt-&gt;executeQuery();
  
          while(res-&gt;next())
          {
              if (res-&gt;getString("password") == Password)
              {
                  cred = true;
              }
          }
      }
      catch(sql::SQLException &amp;e) {
          ManageException(e);
          return false;
      }
  
      return cred;
  }
  </code></pre>
  
  <p>Basically, It compiles without a problem, Connects without a problem, Executes queries without a problem, but the second I try to retrieve data some breakpoint exception is thrown in a file "xutils.cpp". I really have no idea what I'm doing wrong. I'm using the DEBUG libraries while compiling for debug. Hmm libmysql.dll should be release since I extracted it from the server bundle, but I don't seem to find it as a source to compile my own. </p>
  
  <p>I really have no idea why it crashes and burn like that :/ </p>
  
  <p>PS: Don't mind the no hashing of the password, it really is just a proof of concept to me in the way of ... getting it to work first, then securing it :U</p>
  
  <p>PS: I also have Boost libraries compiled and ready in the project, if that would help :U</p>
  
  <p>EDIT: Main function</p>
  
  <pre><code>bool ServerRunning = true;
  int main(int argc, char** argv)
  {
      #ifdef _WIN32
          std::string title = TEXT("Window Title Change");
          SetConsoleTitle(title.c_str());
      #endif;
  
      std::cout&lt;&lt;"Loading Configuration File..."&lt;&lt;std::endl&lt;&lt;std::endl;
  
      std::string path = boost::filesystem::path(boost::filesystem::current_path()).string();
      path += "\\Config.ini";
  
      INIParser* Config = new INIParser(path.c_str()); //MinINI
  
      // Sockets data
      std::string listenIP = Config-&gt;GetString("Network", "ListenIP", "127.0.0.1");
      unsigned short listenPort = Config-&gt;GetInt("Network", "ListenPort", 5000);
  
      // Database data
      std::string dbHost = Config-&gt;GetString("Database", "Host", "localhost");
      std::string dbUser = Config-&gt;GetString("Database", "User", "root");
      std::string dbPass = Config-&gt;GetString("Database", "Password", "");
      std::string dbName = Config-&gt;GetString("Database", "Database", "authserv");
      unsigned short dbPort = Config-&gt;GetInt("Database", "Post", 1000);
  
      // General settings
      int sessionTimeout = Config-&gt;GetInt("Settings", "SessionTimeout", 10);
      int maxClients = Config-&gt;GetInt("Settings", "MaxClients", 10);
      int serverTimeout = Config-&gt;GetInt("Settings", "GameserverTimeout", 1);
  
       // Begin Initialization
       DBManager::Instance()-&gt;Connect(dbHost.c_str(), dbPort, dbUser.c_str(), dbPass.c_str(), dbName.c_str());
       bool loginSuccess = DBManager::Instance()-&gt;ValidCredentials("Username", "Password");
  
      char c;
      while (ServerRunning)
      {
          std::cin&gt;&gt;c;
  
          if (c == 'q')
          {
              ServerRunning = false;
          }
      }
  
      return 0;
  }
  </code></pre>