问题描述
我的代码如下:
main.ccint 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;
}
如上图所示,指针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:
- 我的Ubuntu版本是Ubuntu-16.04,而lib版本是libMysqLclient.so.20。
- sqlConnectionPool_test.cc在多线程环境中运行。原因是该测试文件更有可能崩溃,因为MysqL_init不是线程安全的。但是它可以完美地工作。
- 如果需要更多信息,请告诉我,谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)