Hi I am having trouble trying to connect to a local MySql db using the MySQL Connector/C++. I was able to successfully compile the bellow code but when I run it results in a Segmentation fault. I am trying to run this on a Windows XP machine using the g++ compiler through cygwin with NetBeans as the IDE.

#include <stdlib.h>
#include <iostream>

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

using namespace std;

int main(int argc, const char *argv[]){
    cout << "test";
    sql::Driver *driver;
    sql::Connection *con;
    sql::Statement *stmt;
    sql::ResultSet *res;
    sql::PreparedStatement *pstmt;

    try{
        driver = get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "root", "test");
        con->setSchema("test");
        delete con;
    }catch(sql::SQLException &e){
        std::cout<<e.what();
    }
}

Also just in case it helps here the the output when I tried the gdb command

$ gdb mysqltest.exe
GNU gdb (GDB) 7.3.50.20111026-cvs (cygwin-special)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-cygwin".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /cygdrive/c/Documents and Settings/aaron_salo/My Documents/
NetBeansProjects/mysqlTest/dist/Release/Cygwin_4.x-Windows/mysqltest.exe...done.

(gdb) run
Starting program: /cygdrive/c/Documents and Settings/aaron_salo/My Documents/Net
BeansProjects/mysqlTest/dist/Release/Cygwin_4.x-Windows/mysqltest.exe
[New Thread 4488.0x1304]
[New Thread 4488.0x16fc]

Program received signal SIGSEGV, Segmentation fault.
0x1001a667 in mysqlcppconn!??CSQLString@sql@@QAEPAV?$basic_string@DU?$char_trait
s@D@std@@V?$allocator@D@2@@std@@XZ ()
from /cygdrive/c/Program Files/MySQL/MySQL Connector C++ 1.1.0/lib/opt/mysqlc
ppconn.dll
(gdb) where
#0 0x1001a667 in mysqlcppconn!??CSQLString@sql@@QAEPAV?$basic_string@DU?$char_t
raits@D@std@@V?$allocator@D@2@@std@@XZ ()
from /cygdrive/c/Program Files/MySQL/MySQL Connector C++ 1.1.0/lib/opt/mysqlc
ppconn.dll
#1 0xa512c5c6 in ?? ()
#2 0x0023ccb0 in ?? ()
#3 0x61007038 in _cygwin_exit_return ()
from /cygdrive/c/cygwin/bin/cygwin1.dll
#4 0x00000001 in ?? ()
#5 0x100c80e0 in ?? ()
#6 0x00000000 in ?? ()
(gdb)

Thanks for your help any incite into what could be causing the segmentation fault would be much appreciated.

Line 14-18 set all pointers to null.
Line 21 check if driver is null
comment out line 23 and see if it runs fine.

I updated the code but with not luck, any other thoughts as to what could cause it?

just in case I made a mistake when I updated it here is the new version

#include <stdlib.h>
#include <iostream>
     
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
     
using namespace std;
     
int main(int argc, const char *argv[]){
    cout << "test";
    sql::Driver *driver = NULL;
    sql::Connection *con = NULL;
    sql::Statement *stmt = NULL;
    sql::ResultSet *res = NULL;
    sql::PreparedStatement *pstmt = NULL;
     
    try{
        driver = get_driver_instance();
        if(driver!=NULL)
                con = driver->connect("localhost", "root", "test");
        //con->setSchema("test");
        delete con;
    }catch(sql::SQLException &e){
        std::cout<<e.what();
    }
    return 0;
}

Hi Lamborghini2121,

It turned out to be a problem with my development environment not the code it self. I switched to Microsoft Visual C++ 2010 Express and followed the guide on how to set it up properly which worked perfectly.

My only guess is the mixing of windows libraries with a unix like compiler that i was previously using was a bad idea.

I hope that helps.

This question has already been answered. Start a new discussion instead.