2016-09-19 46 views
-2

这里是代码片断:STL vector.clear()原因存储器双免费或腐败

pthread_mutex_lock(&hostsmap_mtx); 
for (int i = 0; i < service_hosts[service].size(); ++i) 
    poolmanager->delPool(service, service_hosts[service][i].first); 

service_hosts[service].clear(); 
for (int i = 0; i < servers->count; ++i) { 
    string temp(servers->data[i]); 
    int pos = temp.find(':'); 
    string server = temp.substr(0, pos); 
    string port = temp.substr(pos + 1, temp.length() - pos - 1); 
    service_hosts[service].push_back(make_pair(server, atoi(port.c_str()))); 

    config.server = server; 
    config.port = atoi(port.c_str()); 

    poolmanager->addPool(service, config); 
} 

pthread_mutex_unlock(&hostsmap_mtx); 

类型service_hosts的是map<string, vector<pair<string, int> > >

崩溃原因:
*错误在” ./HttpProxy “:双重释放或腐败(fasttop):0x00007f6fe000a6b0 *

和gdb BT:

5 ~basic_string (this=0x7f6fe0000960, __in_chrg=<optimized out>) 
    at /usr/include/c++/4.8.3/bits/basic_string.h:539 
6 ~pair (this=0x7f6fe0000960, __in_chrg=<optimized out>) 
    at /usr/include/c++/4.8.3/bits/stl_pair.h:96 
7 _Destroy<std::pair<std::basic_string<char>, int> > (__pointer=0x7f6fe0000960) 
    at /usr/include/c++/4.8.3/bits/stl_construct.h:93 
8 __destroy<std::pair<std::basic_string<char>, int>*> (__last=<optimized out>, 
    __first=0x7f6fe0000960) at /usr/include/c++/4.8.3/bits/stl_construct.h:103 
9 _Destroy<std::pair<std::basic_string<char>, int>*> (__last=<optimized out>, 
    __first=<optimized out>) at /usr/include/c++/4.8.3/bits/stl_construct.h:126 
10 _Destroy<std::pair<std::basic_string<char>, int>*, std::pair<std::basic_string<char>, int> > (
    __last=0x7f6fe0000970, __first=0x7f6fe0000960) 
    at /usr/include/c++/4.8.3/bits/stl_construct.h:151 
11 _M_erase_at_end (this=<optimized out>, __pos=0x7f6fe0000960) 
    at /usr/include/c++/4.8.3/bits/stl_vector.h:1352 
12 clear (this=0x7f6fe000a0f8) at /usr/include/c++/4.8.3/bits/stl_vector.h:1126 

任何意见将不胜感激。

+2

我的建议:make a [mcve]。如果仅凭这一点不会帮助您至少发现该错误,则可以让其他人帮助您 – user463035818

+0

如果您在幕后使用libcurl,请注意您需要采取一些额外步骤以使您的代码线程安全: https://curl.haxx.se/libcurl/c/threadsafe.html。特别检查“TLS”标题,看看你是否使用OpenSSL并检查是否必须实现必要的锁定。如果这是你需要的,请回复,如果需要更多的信息。 –

+0

@万宝路人只使用动物园管理员,我试图找出原因,但其原因太奇怪了,如果我评论'service_hosts [service] .clear();',那么它不会崩溃。 – EmilyAvon

回答

0

这个问题可能并不容易找出根本原因。但最后我找出原因。原因stl string不是线程安全的,并且stl string赋值是引用计数和COW。例如:

string str = "stl::string"; 
string temp = str; //this is ref-count,COW 
string temp2 = str.c_str(); //this is memory copy, cause string don't know how to handle const char *, just copy it 

我的解决方法是通过const char *到STL vecotrs,并在多线程条件下使用const char *作为函数参数,如果有一争。例如:

map<string, vector<pair<string, int> > > Hostmap; 
string service = "service"; 
string host = "host"; 
//Note: not service and host, but service.c_str() and host.c_str() 
Hostmap[service.c_str()].push_back(make_pair(host.c_str(), 9966)); 

希望这个问题能为您遇到的问题提供一些提示。

0

使用valgrind(或您有权访问的任何其他类似工具)可以非常容易地诊断双重空闲。它会告诉你谁释放了你正在访问的内存,这会将你引向问题的根源。如果您在阅读valgrind输出时遇到问题,请在此处发帖,我们可以帮助您解决问题。

+0

感谢您的意见:) – EmilyAvon