Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分段故障。地址0x0不是堆栈、malloc或(最近)空闲 我对C++很陌生,但我有办法解决这个问题。如果你能帮我做这件事,我将不胜感激_C++_Gcc_Segmentation Fault_Malloc - Fatal编程技术网

分段故障。地址0x0不是堆栈、malloc或(最近)空闲 我对C++很陌生,但我有办法解决这个问题。如果你能帮我做这件事,我将不胜感激

分段故障。地址0x0不是堆栈、malloc或(最近)空闲 我对C++很陌生,但我有办法解决这个问题。如果你能帮我做这件事,我将不胜感激,c++,gcc,segmentation-fault,malloc,C++,Gcc,Segmentation Fault,Malloc,它是一种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=

它是一种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]可以包含空指针。这意味着当您将其推回到字符串向量上时,std::string将使用空指针初始化。这是合法的,但却是致命的错误

也许试试这样的

或者这个:


可以猜测的代码非常少,但我认为:

while(cityList.size()>0){
    string city = cityList[cityList.size()-1];
    insertMatchByCity(conn,city);
    cityList.erase(cityList.end()-1);
}
如果您通过引用将city发送到insertMatchByCity,则该线程可能指向另一个线程,并且该线程尚未处理其数据,即它仍然持有对city的引用。假设线程被操作系统抢占。 现在,您的迭代while循环将继续,作为局部范围变量的city将被销毁。 所以,现在当该线程尝试取消引用city时,它会导致崩溃


应用我的法医学分析:

没有产生错误的代码的错误只是故事的一半。胡乱猜测。您是否执行以下操作:std::string s=0;?main.cpp中哪一行是759?@jrok insertMatchByCityconn,city;是第759行。我怀疑:第[0]行可能包含空指针,调用我上面的猜测错误。非常感谢您的建议,但恐怕这两个代码都不起作用。请尝试添加fprintfstderr,第[0]行=%p\n,第[0]行;在循环的开头-如果您看到它在崩溃前打印出第[0]=0行,您就知道空指针是您的问题。@Brianc您能指出哪一行显示了错误吗?@JeremyFriesner我添加了代码,它显示了第[0]=0xa18b48行[0]=0xa18b78行[0]=0xa18ba8行[0]=0xa18bd8行[0]=0xa18bd8行[0]=0xa18c08行[0]=0xa18c38行[0]=0xa18c68行[0]=0xa18c98行[0]=0xa18cc8行[0]=0xa18d00行[0]=0xa18d30行[0]=0xa18d60行[0]=0xa18d90@Brianc我的意思是你的原始代码中的哪一行。如果我们知道哪一行触发了失败,这将有助于推断原因。谢谢你的建议。但直到昨天晚上,它才完美地工作。你认为系统的一些变化会影响跑步吗?这些被称为比赛条件,随时可能出现。我见过系统在惨遭崩溃前运行了几年。我有预感,我指明的根本原因是罪魁祸首。在这种情况下,更改insertMatchByCityconn,城市;接受城市作为值,而不是参考
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
}
while(cityList.size()>0){
    string city = cityList[cityList.size()-1];
    insertMatchByCity(conn,city);
    cityList.erase(cityList.end()-1);
}