为什么在以下代码中mysql_initnullptr导致分段错误?

如何解决为什么在以下代码中mysql_initnullptr导致分段错误?

我的代码如下:

main.cc
int main(void){
    int port,sqlPort,ConnectionNum,threadNum;
    string sqlUser,sqlPwd,dbname;
    try{
        Config Settings("./config.ini");
        Settings.Read("port",port);
        Settings.Read("sqlPort",sqlPort);
        Settings.Read("sqlUser",sqlUser);
        Settings.Read("sqlPwd",sqlPwd);
        Settings.Read("dbname",dbname);
        Settings.Read("ConnectionNum",ConnectionNum);
        Settings.Read("threadNum",threadNum);
    }catch(File_Not_Found& e){
        cerr << "Cannot Find the Configuration File:" << e.what() << endl;
        exit(1);
    }catch(Key_Not_Found& e){
        cerr << "Cannot Find the Keyword:" << e.what() << endl;
        exit(1);
    }

    WebServer server(port,5000,sqlUser,dbname,threadNum );
    //server.Start();
    return 0;
}
WebServer.cc
WebServer::WebServer(int port,int timeoutMS,int sqlPort,const std::string& sqlUser,const std::string& sqlPwd,const std::string& dbname,int connPoolNum,int threadNum)
        :m_port(port),m_timeoutMS(timeoutMS),m_epoller(new Epoller()),m_timer(new TimerHeap()){
            assert(port < 65535 && port > 1024);
            m_srcDir = get_current_dir_name();
            assert(m_srcDir);
            strncat(m_srcDir,"/resources/",16);
            HttpConn::userCnt = 0;
            HttpConn::srcDir = m_srcDir;
            connPool->init("localhost",dbname,connPoolNum);
            threadPool->start(threadNum);
            m_isRunning = InitSocket();
            if(m_isRunning){
                LOG_INFO << "===================== Server Init Success =====================";
                LOG_INFO << "Port : " << m_port << ",srcDir :" << m_srcDir;
                LOG_INFO << "threadNum : " << threadNum << ",ConnectionNum : " << connPoolNum;
                
            }else{
                LOG_FATAL << "Socket Init Failure";
            }
}
sqlConnectionPool.cc
ConnectionPool *ConnectionPool::GetInstance()
{
    static ConnectionPool connPool;
    return &connPool;
}
void ConnectionPool::init(string url,string User,string PassWord,string dbname,int Port,int MaxConn)
{
    m_url = url;
    m_Port = Port;
    m_User = User;
    m_PassWord = PassWord;
    m_DatabaseName = dbname;

    for (int i = 0; i < MaxConn; i++)
    {
        MysqL *con = NULL;
        con = MysqL_init(con);   //segmentation fault happened!!

        if (con == NULL)
        {
            LOG_FATAL << "MysqL Init Error";
        }
        con = MysqL_real_connect(con,url.c_str(),User.c_str(),PassWord.c_str(),dbname.c_str(),Port,NULL,0);

        if (con == NULL)
        {
            LOG_FATAL << "MysqL Get Real Connection Error";
        }
        connList.push_back(con);
        ++m_FreeConn;
    }

    m_MaxConn = m_FreeConn;
}

enter image description here

如上图所示,指针con始终为nullptr,这会导致分段错误。而且我编写了另一个代码来测试sqlConnectionPool.cc文件,但这一次它运行良好。

sqlConnectionPool_test.cc(部分代码
ConnectionPool* connPool = ConnectionPool::GetInstance();
unordered_map<string,string> users;
vector<thread> thread_pool;
int main(void){
    connPool->init("localhost","root","123123","zjwdb",3306,8);
    {
        MysqL *MysqL = NULL;
        connectionRAII MysqLcon(&MysqL,connPool);
        if(MysqL_query(MysqL,"CREATE TABLE user_test(username VARCHAR(100) NOT NULL,passwd VARCHAR(60) NOT NULL,PRIMARY KEY ( username ));")){
            LOG_ERROR << "CREATE TABLE user_test Failed : " << MysqL_error(MysqL);
        }

        if(!Register("test1","test1")){
            LOG_ERROR << "INSERT test user Failed";
        }
        if(!Register("test2","test2")){
            LOG_ERROR << "INSERT test user Failed";
        }
        if(!Register("test3","test3")){
            LOG_ERROR << "INSERT test user Failed";
        }

        showUsers(MysqL);
    }
    CountDownLatch latch(4);
    thread_pool.emplace_back(login,"test1","test1");
    
    thread_pool.emplace_back(login,"test5","test5");
    
    thread_pool.emplace_back(login,"test");
    
    thread_pool.emplace_back([](CountDownLatch& latch,string const& name,string const& passwd)
        {
            latch.countDown();
            latch.wait();
            Register(name,passwd);
            login(name,passwd);
        },std::ref(latch),"test_user1","123123");
    thread_pool.emplace_back([](CountDownLatch& latch,"test_user2","test_user3","test3","test3");

    for(auto& th : thread_pool){
        if(th.joinable())
            th.join();
    }
    MysqL* conn_temp = NULL;
    connectionRAII MysqLconn_temp(&conn_temp,connPool);
    showUsers(conn_temp);
    dropTheTable();
    return 0;
}

那真让我感到困惑。它是libMysqL.lib的错误吗?如果是的话,我该怎么解决呢?
PS:

  1. 我的Ubuntu版本是Ubuntu-16.04,而lib版本是libMysqLclient.so.20。
  2. sqlConnectionPool_test.cc在多线程环境中运行。原因是该测试文件更有可能崩溃,因为MysqL_init不是线程安全的。但是它可以完美地工作。
  3. 如果需要更多信息,请告诉我,谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?