2014-07-01 24 views
0

以下是我爲某個函數編寫的代碼,該函數應該始終在其自己的線程上運行,檢查數據庫以獲取新信息並將新信息附加到全局向量。我似乎遇到的問題是,經過各地這麼多次,都獲得成功,我會自發地得到這個錯誤:線程在檢查數據庫時拋出錯誤

Unhandled exception at 0x5B46F1F9 (libmysql.dll) in Project2.exe: 0xC0000005: Access violation reading location 0x000003B0.

也不太清楚怎麼了,但它似乎發生了if(connect){}聲明之後因爲正確,因爲它崩潰,我看到Connection Failed!打印聲明。我只是沒有理由爲什麼會失敗。我有一個類似的問題,是由於達到數據庫的最大連接數而引起的,但我不知道這將如何發生,因爲在if聲明的每次迭代後,我都使用mysql_close(connect)聲明。

void getNewFromDB(){ 
while(globalExit == false){ 
    MYSQL *connect; // Create a pointer to the MySQL instance 
    connect=mysql_init(NULL); // Initialise the instance 
    /* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/ 
    if(!connect) /* If instance didn't initialize say so and exit with fault.*/ 
    { 
     fprintf(stderr,"MySQL Initialization Failed"); 


    } 
    /* Now we will actually connect to the specific database.*/ 

    connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0); 
    /* Following if statements are unneeded too, but it's worth it to show on your 
    first app, so that if your database is empty or the query didn't return anything it 
    will at least let you know that the connection to the mysql server was established. */ 

    if(connect){ 
     printf("Connection Succeeded\n"); 
    } 
    else{ 
     printf("Connection Failed!\n"); 
    } 


     MYSQL_RES *result; /* Create a pointer to recieve the return value.*/ 
     MYSQL_ROW row; /* Assign variable for rows. */ 

     mysql_query(connect,"SELECT * FROM locationTime ORDER BY id DESC"); 
     /* Send a query to the database. */ 

     result = mysql_store_result(connect); /* Receive the result and store it in res_set */ 

     unsigned int numrows = mysql_num_rows(result); /* Create the count to print all rows */ 

     row = mysql_fetch_row(result); 
     if(row[0] > ids.back()){ 
      ids.push_back(row[0]); 
      dateTime.push_back(row[1]); 
      locs.push_back(setLocation(row[2])); 
      imgPaths.push_back(row[3]); 
     } 
mysql_close(connect); 
    } 
     /* Close and shutdown */ 

} 

編輯:所有給出的答案解決這個問題的一部分,我感謝你。但是現在我不明白的是,爲什麼在16000個請求之後突然間連接失敗?

+0

這很可能是一個'null'指針AV。也許你的一個MySQL調用中的一個參數不允許爲'null'?最有可能的嫌疑是'mysql_query(connect,...)'連接'' – JensG

+0

'mysql_real_connect'是否成功或失敗,你仍然跋涉。所有的代碼都應該移到'if(connect)'塊中。 –

回答

1
MYSQL *connect; // Create a pointer to the MySQL instance 
connect=mysql_init(NULL); // Initialise the instance 
/* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/ 
if(!connect) /* If instance didn't initialize say so and exit with fault.*/ 
{ 
    fprintf(stderr,"MySQL Initialization Failed"); 
} 

的代碼部分之後,大約有失敗的消息,你的「連接」未初始化,因此您收到訪問衝突,因爲你把「連接」的功能。沒有初始化,它可以指向虛擬內存的任何部分。這就是爲什麼你有問題。

修復這樣說:

MYSQL *connect = NULL; // Create a pointer to the MySQL instance 
connect=mysql_init(NULL); // Initialise the instance 
/* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/ 
if(!connect) /* If instance didn't initialize say so and exit with fault.*/ 
{ 
    fprintf(stderr,"MySQL Initialization Failed"); 
    return; 
} 

而且你應該用你的庫的非標準的方法來打印錯誤號和錯誤的原因,明白髮生了什麼。 用途:

fprintf(stderr, "%s\n", mysql_error(connect)); 

UPD:你也可能有內存泄漏:

connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0); 

所以,這是從

connect=mysql_init(NULL); 

線收你從來沒有免費的連接對象。您只需通過指向由mysql_real_connect返回的連接的指針來寫入指針即可。

這就是爲什麼在成功16k後,您可能會因爲內存不足而分配失敗。 It表示返回的指針指向第一個參數的副本。

+0

所有給出的答案都解決了問題的一部分,我感謝你。但是現在我不明白的是,爲什麼在16000個請求之後突然間連接失敗? – Darksaint2014

+0

我更新了答案。 MSSQL,正如這裏指出的:http://dev.mysql.com/doc/refman/5.0/en/mysql-init.html,如果有內存問題,可以返回NULL。它也必須由mysql_close()釋放,但你不這樣做。 – Arkady

+0

我使用mysql_close(connect)關閉連接變量;聲明在所有右括號之前。這個關閉聲明如果放在哪裏? – Darksaint2014

0

如果連接爲false,則會打印「連接失敗」,但之後無論如何都會在查詢調用中使用無效連接對象。

相關問題