日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

关于std::string 在 并发场景下 __grow_by_and_replace free was not allocated 的异常问题

發布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于std::string 在 并发场景下 __grow_by_and_replace free was not allocated 的异常问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用string時發現了一些坑。
我們知道stl 容器并不是線程安全的,所以在使用它們的過程中往往需要一些同步機制來保證并發場景下的同步更新。

應該踩的坑還是一個不拉的踩了進去,所以還是記錄一下吧。
string作為一個容器,隨著我們的append 或者 針對string的+ 操作都會讓string內部的數據域動態增加,而動態增加的過程則伴隨著一些局部指針變量的創建和釋放,而當我們并發對同一個string進行操作的時候(測試很明顯,寫工程項目因為專注于各個模塊細節,這一些問題因為代碼功底不夠,還是沒有辦法注意到位),就會出現一些double-free 這樣的異常問題(double-free 即 對同一個地址釋放了兩次,第一次對一個地址free的時候這段內存已經還給了操作系統,當第二次訪問這個地址則就是非法訪問了)。

看看下面這個測試代碼,大體邏輯就是多線程從一個已有的string數組中并發將數組中的內容取出編碼到一個全局的string里面。

#include <iostream>#include <string.h>
#include <thread>
#include <unistd.h>
#include <vector>#include <assert.h>using namespace std;std::vector<std::string> data_vec;
std::string dst;char* EncodeVarint32(char* dst, uint32_t v) {// Operate on characters as unsignedsuint8_t* ptr = reinterpret_cast<uint8_t*>(dst);static const int B = 128;if (v < (1 << 7)) {*(ptr++) = v;} else if (v < (1 << 14)) {*(ptr++) = v | B;*(ptr++) = v >> 7;} else if (v < (1 << 21)) {*(ptr++) = v | B;*(ptr++) = (v >> 7) | B;*(ptr++) = v >> 14;} else if (v < (1 << 28)) {*(ptr++) = v | B;*(ptr++) = (v >> 7) | B;*(ptr++) = (v >> 14) | B;*(ptr++) = v >> 21;} else {*(ptr++) = v | B;*(ptr++) = (v >> 7) | B;*(ptr++) = (v >> 14) | B;*(ptr++) = (v >> 21) | B;*(ptr++) = v >> 28;}return reinterpret_cast<char*>(ptr);
}inline void PutVarint32(std::string* dst, uint32_t v) {char buf[5];char* ptr = EncodeVarint32(buf, v);// If the v is a negative number, we need store the last byte.dst->append(buf, static_cast<size_t>(ptr - buf));
}inline void PutLengthPrefixedSlice(std::string* dst, const std::string& value) {PutVarint32(dst, static_cast<uint32_t>(value.size()));dst->append(value.data(), value.size());
}void EncodeTo(std::string* dst, std::string key) {PutLengthPrefixedSlice(dst, key);
}void EncodeDataVec() {std::cout << "Encode data_vec" << std::endl;for (int i = 0;i < data_vec.size(); i ++) {EncodeTo(&dst, data_vec[i]);}
}void ConstructDataVec() {std::cout << "Construct data_vec" << std::endl;for (int i = 0;i < 10; i++) {data_vec.emplace_back(std::to_string(i));}
}int main(int argc, char* argv[]) {int threads = 1;if (argc == 2) {threads = atoi(argv[1]);}std::cout << "threads : " << threads << std::endl;ConstructDataVec();for (int i = 0;i < threads; i++) {new std::thread(EncodeDataVec);}return 0;
}

當我設置并發數為20的時候,很明顯出現如下問題

./concurrent_test 20## 異常問題,大體就是釋放了一個不存在的地址
concurrent_test(5008,0x700008738000) malloc: *** error for object 0x7fefab504080: pointer being freed was not allocated
concurrent_test(5008,0x700008738000) malloc: *** set a breakpoint in malloc_error_break to debug
[1]    5008 abort      ./concurrent_test 20

lldb一下看看:

lldb ./concurrent_test 20
(lldb) target create "./concurrent_test"
Current executable set to '/Users/zhanghuigui/Desktop/work/source/cpp_practice/data_structure/string/concurrent_test' (x86_64).
(lldb) settings set -- target.run-args  "20"
(lldb) r
Process 5828 stopped
* thread #4, stop reason = signal SIGABRTframe #0: 0x00007fff2030c462 libsystem_kernel.dylib`__pthread_kill + 10
libsystem_kernel.dylib`__pthread_kill:
->  0x7fff2030c462 <+10>: jae    0x7fff2030c46c            ; <+20>0x7fff2030c464 <+12>: movq   %rax, %rdi0x7fff2030c467 <+15>: jmp    0x7fff203066a1            ; cerror_nocancel0x7fff2030c46c <+20>: retq  
(lldb) bt

異常棧的信息如下

core在了grow_by_and_replace中,這個函數被string::append函數調用,也就是我們上面測試代碼底層的Encode邏輯會調用這個append,然后grow_by_and_replace就是為了對容器進行擴容。

基本實現代碼如下:

我們可以發現在grow_by_and_replace 在實現擴容的邏輯過程中需要分配新的地址空間,將舊的數據拷貝到新的地址,這個過程需要借用臨時指針,并且在完成拷貝之后釋放老的地址old_p
很明顯,我們并發append全局string的時候,這里的old_p 的釋放并不是線程安全的,兩個線程同時append,且都需要進行擴容,則一個擴容完成釋放舊指針,但是舊指針還在被另一個線程引用,則第二個線程擴容完成釋放舊指針,顯然是訪問了一個空的地址了。

除了并發問題之外,使用string 不斷得append的時候 還會有性能問題,因為append擴容期間 會不斷得有數據拷貝,而內存拷貝是很浪費時間的,所以string使用時如果能夠預知容量,建議reserve 足夠的空間,能夠避免動態分配空間造成的性能問題,當然,如果提前reserve的話 也不會有 grow_by_and_replace 這個問題的。

main函數中,調用線程邏輯之前增加dst.reserve(10000),則并發100線程 跑100輪都不會有問題了。

但是在我們實際的應用過程中想要解決 這個string 并發擴容時造成的內存泄漏問題,我們還需要有其他的辦法。

局部構造好之后賦值給一個全局變量std::string即可:

void EncodeDataVec() {std::cout << "Encode data_vec" << std::endl;std::string tmp_dst;for (int i = 0;i < data_vec.size(); i ++) {EncodeTo(&tmp_dst, data_vec[i]);}dst = std::string(tmp_dst);
}

總結

以上是生活随笔為你收集整理的关于std::string 在 并发场景下 __grow_by_and_replace free was not allocated 的异常问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。