Double delete in initializer_list vs 2013(初始化程序列表与 2013 中的双重删除)
问题描述
今天在我的项目中遇到内存问题,一个类使用 c++ 11 initializer_list.系统发出内存问题信号:dbgdel.cpp 中的表达式 _BLOCK_TYPE_IS_VALID(pHead->nBlockUse).我将代码简化为一个简单示例,它不再抛出表达式,但问题从调试输出中变得明显.在我看来,这代码是正确的,它似乎也适用于 g++.
#include
#include <内存>#include <字符串>#include #include <向量>#include <地图>#include #include <initializer_list>使用命名空间标准;B类{上市:字符数据[256];B(const string& x) {cout<<初始化"<<这<<结束;}B(const B& b) {cout<<复制"<<这<<结束;}~B() {cout<<删除 b" <<这<<结束;}};C类{上市:向量 Bbs;C(initializer_list<B> bb) {for(auto& b : bb) {bs.push_back(b);}}};int main(int argc, char** argv) {C bb { B("foo"), B("bar") };返回0;} 输出为:
<前>初始化 00B7FAE8初始化 00B7FBE8复制 00E108A0复制 00E10AE8 (?????)删除 b 00E108A0复制 00E10BE8删除 b 00B7FBE8删除 b 00B7FAE8删除 b 00B7FAE8(删除两次!)
我在这里犯了什么错误或者这不应该工作?
initializer_list 的行为有问题.在其析构函数中,它调用整个范围的向量删除(delete[]),然后再次删除数组中的第一个条目.此行为不是 initializer_list 类的一部分,看起来像是编译器错误.initializer_list 没有析构函数,也不分配用于列表的数组.它看起来就像一个 C 数组的包装器.
至于使用您看到的额外副本,这是由向量在初始化期间调整大小引起的.这是您的流程:
Init 00B7FAE8//构造foo"Init 00B7FBE8//构造bar"Copy 00E108A0//将foo"复制到向量(容量=1)Copy 00E10AE8 (?????)//将上述对象复制到调整大小的向量(容量 = 2)Deleting b 00E108A0//删除较小的向量缓冲区Copy 00E10BE8//将bar"从初始化列表复制到向量删除 b 00B7FBE8//以相反的顺序删除初始化列表.这是酒吧"删除 b 00B7FAE8//最后删除.这是foo"删除 b 00B7FAE8(错误)//后来 C::bs 被销毁
您在这里可以看到,由于复制,通过 push_back 初始化向量的速度非常慢.即使您使用了更优雅的方式,也会发生这种情况:
C(initializer_list bb) : bs(bb) {}
一种更快(没有额外副本)的方法是:
C(initializer_list bb) {bs.reserve(bb.size());bs.insert(bs.end(), bb.begin(), bb.end());}
Today in run into a memory problem in my project, with a class using c++ 11 initializer_list. The system signals a memory problem: "Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) in dbgdel.cpp. I simplified the code to a simple example, it no longer throws an expression but the problem becomes apparent from the debug output. In my eyes this code is correct, also it seems to work with g++.
#include <functional>
#include <memory>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
#include <initializer_list>
using namespace std;
class B {
public:
char data[256];
B(const string& x) {
cout << "Init " << this << endl;
}
B(const B& b) {
cout << "Copy " << this << endl;
}
~B() {
cout << "Deleting b " << this << endl;
}
};
class C {
public:
vector<B> bs;
C(initializer_list<B> bb) {
for(auto& b : bb) {
bs.push_back(b);
}
}
};
int main(int argc, char** argv) {
C bb { B("foo"), B("bar") };
return 0;
}
The output is:
Init 00B7FAE8 Init 00B7FBE8 Copy 00E108A0 Copy 00E10AE8 (?????) Deleting b 00E108A0 Copy 00E10BE8 Deleting b 00B7FBE8 Deleting b 00B7FAE8 Deleting b 00B7FAE8 (Deleted twice!)
What mistake I make here or is this not supposed to work?
The initializer_list behavior is buggy. In its destructor it calls a vector delete (a delete[]) of the entire range and then deletes the first entry in the array again. This behavior is not part of the initializer_list class and looks like a compiler bug. initializer_list doesn't have a destructor and doesn't allocate the array used for the list. It just looks like a wrapper for a C array.
As for using the extra copy you see, it's caused by the vector resizing from during its initialization. Here's your flow:
Init 00B7FAE8 // construct "foo"
Init 00B7FBE8 // construct "bar"
Copy 00E108A0 // copy "foo" to vector (capacity=1)
Copy 00E10AE8 (?????) // copy the above object to the resized vector (capacity = 2)
Deleting b 00E108A0 // delete the smaller vector buffer
Copy 00E10BE8 // copy "bar" from initialization_list to vector
Deleting b 00B7FBE8 // delete initialization_list in reverse order. this is "bar"
Deleting b 00B7FAE8 // last to delete. this is "foo"
Deleting b 00B7FAE8 (bug)
// later C::bs is destroyed
What you can see here is the initializing a vector via push_back is quite slow due to copying. This would be happen even if you've used the more elegant way:
C(initializer_list<B> bb) : bs(bb) {}
A faster (no extra copies) method is:
C(initializer_list<B> bb) {
bs.reserve(bb.size());
bs.insert(bs.end(), bb.begin(), bb.end());
}
这篇关于初始化程序列表与 2013 中的双重删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:初始化程序列表与 2013 中的双重删除
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01