2016-09-19 84 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