Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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
C++ STL vector.clear()导致内存双重释放或pthreads程序损坏_C++_Stl_Pthreads - Fatal编程技术网

C++ STL vector.clear()导致内存双重释放或pthreads程序损坏

C++ STL vector.clear()导致内存双重释放或pthreads程序损坏,c++,stl,pthreads,C++,Stl,Pthreads,下面是代码片段: 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->coun

下面是代码片段:

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);
pthread\u mutex\u lock(&hostsmap\u mtx);
对于(int i=0;idelPool(服务,服务\u主机[service][i]。第一);
服务_承载[service].clear();
对于(int i=0;icount;++i){
字符串温度(服务器->数据[i]);
int pos=temp.find(“:”);
字符串服务器=临时子服务器(0,位置);
字符串端口=temp.substr(位置+1,温度长度()-pos-1);
服务\u主机[service]。推回(创建\u对(服务器,atoi(port.c_str()));
config.server=server;
config.port=atoi(port.c_str());
池管理器->添加池(服务,配置);
}
pthread_mutex_unlock(&hostsmap_mtx);
主机的服务类型为
map

崩溃原因:
“/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  
5~基本字符串(this=0x7f6fe0000960,在chrg=)
at/usr/include/c++/4.8.3/bits/basic_string.h:539
6对(此=0x7f6fe0000960,uuu in_uchrg=)
at/usr/include/c++/4.8.3/bits/stl_pair.h:96
7_销毁(u指针=0x7f6fe0000960)
at/usr/include/c++/4.8.3/bits/stl_construct.h:93
8 uuu销毁(uuu last=,
__first=0x7f6fe0000960)at/usr/include/c++/4.8.3/bits/stl_construct.h:103
9(上次=,,
__first=)at/usr/include/c++/4.8.3/bits/stl_construct.h:126
10!(
__最后一个=0x7f6fe0000970,第一个=0x7f6fe0000960)
at/usr/include/c++/4.8.3/bits/stl_construct.h:151
11 \u M\u在\u端擦除\u(此=,\u位置=0x7f6fe0000960)
at/usr/include/c++/4.8.3/bits/stl_vector.h:1352
12在/usr/include/c++/4.8.3/bits/stl_vector.h:1126处清除(该值=0x7f6fe000a0f8)

任何建议都将不胜感激。

使用valgrind(或您可以访问的任何其他类似工具)很容易诊断双自由度。它将告诉您谁释放了您正在访问的内存,这将引导您找到问题的根源。如果您在阅读valgrind输出时遇到问题,请将其张贴在此处,我们可以帮助您解决。

这个问题可能不容易找到根本原因。但最后我找到了原因。原因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));
map-Hostmap;
字符串service=“service”;
字符串host=“host”;
//注意:不是服务和主机,而是service.c_str()和host.c_str()
Hostmap[service.c_str()],向后推(生成配对(host.c_str(),9966));

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

我的建议:做一个简单的回答。如果仅此一点无法帮助您找到bug,那么至少其他人可以帮助您如果您在幕后使用libcurl,请注意您需要采取一些额外的步骤来确保代码线程安全:。特别检查“TLS”标题,查看是否使用OpenSSL,并检查是否必须实现必要的锁。如果这是您需要的,请一定要问回来是否需要更多信息。@使用了万宝路纯人类动物园管理员,我正在试图找出原因,但如果我评论
service\u hosts[service].clear(),原因太奇怪了,则不会崩溃。感谢您的建议:)