我对C ++很陌生,但我有点必须解决这个问题。如果你能帮我做,真的很感激。
这是一种每天运行一次的cron程序,直到今天才运行良好。但它向我展示了一个段错误。它从mysql获取用户信息并按城市进行一些匹配并插入到mysql上的表中。所以我跑了valgrind以获得更多信息,如下所示。
==11897== Invalid read of size 1
==11897== at 0x4C28F52: strlen (mc_replace_strmem.c:403)
==11897== by 0x5BF614B: std::string::operator=(char const*) (in /usr/lib64/libstdc++.so.6.0.13)
==11897== by 0x4039E7: insertMatchByCity(st_mysql*, std::string) (main.cpp:156)
==11897== by 0x407DB5: main (main.cpp:759)
==11897== Address 0x0 is not stack'd, malloc'd or (recently) free'd
还有这个
==11897== LEAK SUMMARY:
==11897== definitely lost: 0 bytes in 0 blocks
==11897== indirectly lost: 0 bytes in 0 blocks
==11897== possibly lost: 321,326 bytes in 9,167 blocks
==11897== still reachable: 609,929 bytes in 1,886 blocks
==11897== suppressed: 0 bytes in 0 blocks
==11897== Reachable blocks (those to which a pointer was found) are not shown.
==11897== To see them, rerun with: --leak-check=full --show-reachable=yes
==11897==
==11897== ERROR SUMMARY: 9 errors from 9 contexts (suppressed: 6 from 6)
==11897==
==11897== 1 errors in context 1 of 9:
==11897== Invalid read of size 1
==11897== at 0x4C28F52: strlen (mc_replace_strmem.c:403)
==11897== by 0x5BF614B: std::string::operator=(char const*) (in /usr/lib64/libstdc++.so.6.0.13)
==11897== by 0x4039E7: insertMatchByCity(st_mysql*, std::string) (main.cpp:156)
==11897== by 0x407DB5: main (main.cpp:759)
==11897== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==11897==
--11897--
--11897-- used_suppression: 4 U1004-ARM-_dl_relocate_object
--11897-- used_suppression: 2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a
==11897==
==11897== ERROR SUMMARY: 9 errors from 9 contexts (suppressed: 6 from 6)
Segmentation fault
而msg的另一部分告诉我,下面的源代码需要修改
//get open city list
vector<string> cityList;
sql = "SELECT distinct cityname FROM citylist WHERE open=1";
if(mysql_query(conn,sql.c_str())){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
res = mysql_store_result(conn);
while((row = mysql_fetch_row(res))!=NULL){
cityList.push_back(row[0]);
}
mysql_free_result(res);
//match according city
while(cityList.size()>0){
string city = cityList[cityList.size()-1];
insertMatchByCity(conn,city);
cityList.erase(cityList.end()-1);
}
任何了解这一点的人。请给我一个轻松的指示?
非常感谢你提前
答案 0 :(得分:1)
猜测的代码数量非常少,但是,这就是我的想法:
while(cityList.size()>0){
string city = cityList[cityList.size()-1];
insertMatchByCity(conn,city);
cityList.erase(cityList.end()-1);
}
如果您通过引用发送city到insertMatchByCity(可能是另一个线程),并且该线程仍未处理其数据,即它仍然保持对city的引用。让我们说线程被操作系统抢占了。 现在,您的迭代(while循环)将继续,并且将销毁作为本地范围变量的city。 所以,现在当该线程试图取消引用city时,它会导致崩溃!
应用我的取证分析:)
答案 1 :(得分:0)
我认为这可能是您的问题:
while((row = mysql_fetch_row(res))!=NULL){
cityList.push_back(row[0]);
}
根据MySQL Docs您的row[0]
可以包含null
指针。这意味着当您将其推回到字符串向量时,std::string
将使用空指针进行初始化。合法,但是致命的错误。
也许尝试这样的事情?
while((row = mysql_fetch_row(res))!=NULL){
if(row[0])
cityList.push_back(row[0]); // if you don't want blank items
}
或者这个:
while((row = mysql_fetch_row(res))!=NULL){
cityList.push_back(row[0] ? row[0] : ""); // if blank items are ok
}