我的问题是在调试这个功能时出现的。
std::vector<patch_bundle> clean_up(std::vector<patch_bundle> vc){
cout << "girdim ha";
for(auto itr = vc.begin(); itr != vc.end();){
patch_bundle cmp_patch_bundle = *itr;
string cmp_indices = cmp_patch_bundle.patch1_name;
for(auto i_itr = vc.begin(); i_itr != vc.end();){
if(i_itr != itr){
patch_bundle tmp_patch_bundle = *i_itr;
string indices1 = tmp_patch_bundle.patch1_name;
string indices2 = tmp_patch_bundle.patch2_name;
if((indices1 == cmp_indices) || (indices2 == cmp_indices)){
vc.erase(i_itr);
}
else{
++i_itr;
}
}
else{
++i_itr;
}
}
++itr;
}
}
patch_bundle
是struct
,如下:
struct patch_bundle{
int patch1[4] = {0,0,0,0};
int patch2[4] = {0,0,0,0};
std::string patch1_name;
std::string patch2_name;
std::string patch1_label;
std::string patch2_label;
};
这是调试器输出:
Thread 1 hit Breakpoint 1, clean_up (vc=std::vector of length 5400, capacity 5400 = {...}) at test.cpp:338
338 cout << "girdim ha";
(gdb) n
girdim ha339 for(auto itr = vc.begin(); itr != vc.end();){
(gdb)
341 patch_bundle cmp_patch_bundle = *itr;
(gdb)
342 string cmp_indices = cmp_patch_bundle.patch1_name;
(gdb)
343 for(auto i_itr = vc.begin(); i_itr != vc.end();){
(gdb)
344 if(i_itr != itr){
(gdb)
359 ++i_itr;
(gdb)
343 for(auto i_itr = vc.begin(); i_itr != vc.end();){
(gdb)
344 if(i_itr != itr){
(gdb)
345 patch_bundle tmp_patch_bundle = *i_itr;
(gdb)
346 string indices1 = tmp_patch_bundle.patch1_name;
(gdb)
347 string indices2 = tmp_patch_bundle.patch2_name;
(gdb)
**348 if((indices1 == cmp_indices) || (indices2 == cmp_indices)){
**(gdb)
353 ++i_itr;
(gdb)
347 string indices2 = tmp_patch_bundle.patch2_name;
(gdb) n
346 string indices1 = tmp_patch_bundle.patch1_name;
(gdb)
345 patch_bundle tmp_patch_bundle = *i_itr;
(gdb)
344 if(i_itr != itr){
(gdb)
345 patch_bundle tmp_patch_bundle = *i_itr;
(gdb)
346 string indices1 = tmp_patch_bundle.patch1_name;
(gdb)
然后在粗线下方,先读出
indices2
,然后读出indices1
。看起来很奇怪。可能是什么原因?
值得注意的是,我最初只使用标准的
for(int itr = 0; ..;...)
类型的迭代。问题发生在同一粗线之后。所以我的猜测是,记忆方面有些事情不太正确。但这会影响gdb
的行为吗?
如果需要,我希望提供更多信息。
谢谢你。
当您观察到 GDB 进入这些行时:
353 ++i_itr;
(gdb)
347 string indices2 = tmp_patch_bundle.patch2_name;
(gdb) n
346 string indices1 = tmp_patch_bundle.patch1_name;
你看到它执行了
std::string
的析构函数。