2016-10-28 22 views
0

我在這裏得到了一個奇怪的問題,每次當我的程序執行查詢時,網絡因爲某種原因突然不能連接,我的程序退出並打印分段錯誤,趕上例外,但沒有運氣,這裏是一些代碼和錯誤信息,任何想法將不勝感激。由於在查詢期間與MySQL服務器失去連接導致C++程序退出分段錯誤

PS:使用動態庫我的程序與-lmysqlcppconn

# ERR: Lost connection to MySQL server during query (MySQL error code: 2013, SQLState: HY000) 
Segmentation fault (core dumped) 


Debug with gdb. 

Reading symbols from ./http_monitor...done. 
[New LWP 10130] 
[New LWP 10131] 
[New LWP 10132] 
[New LWP 10133] 
[New LWP 10134] 
[New LWP 10200] 
[New LWP 10125] 
[Thread debugging using libthread_db enabled] 
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". 
Core was generated by `./http_monitor'. 
Program terminated with signal SIGSEGV, Segmentation fault. 
#0 0x000000000040715a in CMySQL::query (this=0x1b0d0c0, 
    sql=0x7fb83c000b08 "SELECT file_id,filename,path,upload_time FROM bl_files WHERE upload_time > 0 AND upload_time < 1476966545;", info=0x42a062 "CleanFile notice") at CMySQL.cpp:296 
(gdb) where 
#0 0x000000000040715a in CMySQL::query (this=0x1b0d0c0, 
    sql=0x7fb83c000b08 "SELECT file_id,filename,path,upload_time FROM bl_files WHERE upload_time > 0 AND upload_time < 1476966545;", info=0x42a062 "CleanFile notice") at CMySQL.cpp:296 

部分的源代碼編譯

ResultSet* CMySQL::query(const char* sql, const char* info) 
{ 
    int errorCode = SUCCESS; 
    ResultSet* execResult = NULL; 
    if(mysqlState == NULL || mysqlConnState != TRUE) 
    { 
     return execResult; 
    } 

    pthread_mutex_lock(&mysqlMutex); 
    try 
    { 
     execResult = mysqlState->executeQuery(sql); 
//  DEBUG("CMySQL_update, %d, %s, %s\r\n", execResult, info, sql); 
    } 
    catch (sql::SQLException &e) 
    { 
     /* 
     MySQL Connector/C++ throws three different exceptions: 

     - sql::MethodNotImplementedException (derived from sql::SQLException) 
     - sql::InvalidArgumentException (derived from sql::SQLException) 
     - sql::SQLException (derived from std::runtime_error) 
     */ 
     ERROR_DEBUG("CMySQL_update_error, %d, %s, %s\r\n", execResult, info, sql); 

     cout << "# ERR: SQLException in " << __FILE__; 
     cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; 
     /* what() (derived from std::runtime_error) fetches error message */ 
     cout << "# ERR: " << e.what(); 
     cout << " (MySQL error code: " << e.getErrorCode(); 
     cout << ", SQLState: " << e.getSQLState() << ")" << endl; 

     if(e.getErrorCode() == 1062) 
     {//Duplicate key 
      errorCode = e.getErrorCode(); 
     } 
     else if(e.getErrorCode() == 1064) 
     {//Syntax error 
      errorCode = e.getErrorCode(); 
     } 
     else 
     { 
      errorCode = e.getErrorCode(); 
     } 

     handleLog(e.getErrorCode(), sql, e.what(), info); 
    } 
    catch (exception &e) 
    { 
     ERROR_DEBUG("CMySQL_query_std_exception, %d, %s, %s\r\n", execResult, info, sql); 

     cout << "# ERR: SQLException in " << __FILE__; 
     cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; 
     /* what() (derived from std::runtime_error) fetches error message */ 
     cout << "# ERR: " << e.what() << endl; 

     handleLog(-1, sql, e.what(), info); 
    } 
    catch (...) 
    { 
     ERROR_DEBUG("CMySQL_query_unknown_exception, %s, %s\r\n", info, sql); 

     cout << "# ERR: SQLException in " << __FILE__; 
     cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; 

     handleLog(-2, sql, "unknown exception", info); 
    } 
    pthread_mutex_unlock(&mysqlMutex); 

    return execResult; 
} 

的線296是:execResult = mysqlState->的executeQuery(SQL);

回答

0

如果您在調試信息中使用mysql庫,最好追溯到您可以在CMySQL::query中檢查變量的位置,試圖獲得有關段錯誤的任何線索。

通常,庫代碼在查詢數據庫時應該拋出一個異常,告知連接錯誤,從而爲用戶代碼提供進一步操作的機會。如果您確實想讓程序保持運行,您可以註冊一個處理程序以在Linux中處理SIGSEGV信號。

signal(SIGSEGV, sig_handler);

相關問題