为什么template_back调用析构函数
Why is emplace_back calling destructor?
我有一个类,它有一个已删除的复制构造函数和一个释放C资源的析构函数。
我希望template_back只移动对象并调用析构函数一次,但它是在template_back中调用的,如下面linux上的stl实现所示。为什么会发生这种情况?
结果是C资源被释放了不止一次。
statement(statement&&) = default;
statement& operator=(statement&&) = default;
private:
statement(const statement&) = delete;
statement& operator=(const statement&) = delete;
396│ template<typename _Tp, typename _Alloc>
397│ template<typename... _Args>
398│ void
399│ vector<_Tp, _Alloc>::
400│ _M_emplace_back_aux(_Args&&... __args)
401│ {
402│ const size_type __len =
403│ _M_check_len(size_type(1), "vector::_M_emplace_back_aux");
404│ pointer __new_start(this->_M_allocate(__len));
405│ pointer __new_finish(__new_start);
406│ __try
407│ {
408│ _Alloc_traits::construct(this->_M_impl, __new_start + size(),
409│ std::forward<_Args>(__args)...);
410│ __new_finish = 0;
411│
412│ __new_finish
413│ = std::__uninitialized_move_if_noexcept_a
414│ (this->_M_impl._M_start, this->_M_impl._M_finish,
415│ __new_start, _M_get_Tp_allocator());
416│
417│ ++__new_finish;
418│ }
419│ __catch(...)
420│ {
421│ if (!__new_finish)
422│ _Alloc_traits::destroy(this->_M_impl, __new_start + size());
423│ else
424│ std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
425│ _M_deallocate(__new_start, __len);
426│ __throw_exception_again;
427│ }
428├> std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
429│ _M_get_Tp_allocator());
430│ _M_deallocate(this->_M_impl._M_start,
431│ this->_M_impl._M_end_of_storage
432│ - this->_M_impl._M_start);
433│ this->_M_impl._M_start = __new_start;
434│ this->_M_impl._M_finish = __new_finish;
有两件事没有引起您的注意:
- 从对象中移动的对象仍将被销毁,因此您的移动操作必须传输资源
- 当
vector
增长时,它可能需要重新分配,这是一个4步操作:获取新存储,在新存储中移动(或复制构造)新元素(从旧存储),销毁旧元素,释放旧存储
所以,你的问题只是你没有正确地转移资源;使用std::unique_ptr
作为自定义类的基础,您就不会遇到这样的麻烦。
相关文章:
- 什么时候调用析构函数
- C++-明确何时以及如何调用析构函数
- C++ 防止在映射中放置()时调用析构函数
- 调用析构函数以释放动态分配的内存
- C++:使用方法调用析构函数的顺序是什么?
- 向量推回调用析构函数时调用析构函数
- 如何在调用析构函数时优雅地停止/销毁带有阻塞调用C++线程?
- C++,我应该调用析构函数吗?
- 如何获取有关在 Clang LibTooling 中调用析构函数的信息?
- 当我从 std::vector 中的新放置调用析构函数时会发生什么?
- 为什么这里不调用析构函数
- 在调用 std::bind 的产品后意外调用析构函数
- 为什么在传递给函数而不是构造函数时调用析构函数?
- 如何在C++中调用析构函数
- 为什么为未删除的对象调用析构函数?
- 调用析构函数时出错
- C++ 在不释放内存的情况下调用析构函数
- 为什么在运算符删除中不调用析构函数?
- C++ 调用析构函数后动态模板队列"double free or corruption (out)"
- 在 postOrderDelete 上调用析构函数时引发的异常