我有一个使用std :: deque的结构class VariantWrapper;typedef _STL_NAMESPACE_::deque<VariantWrapper> VariantQueue;struct AttributeValueWrapper{AttributeValueWrapper() : bAttributeIsArray(false){ pVariantQueue = new VariantQueue; if(!pVariantQueue) throw std::bad_alloc();}AttributeValueWrapper(const AttributeValueWrapper& a){ pVariantQueue = a.TakeOwner(); bAttributeIsArray = a.bAttributeIsArray;}AttributeValueWrapper& operator=(AttributeValueWrapper& r){ throw std::bad_exception("equal operator not supported in AttributeValueWrapper");}VariantQueue* TakeOwner() const{ VariantQueue *p = pVariantQueue; pVariantQueue = NULL; return p;}~AttributeValueWrapper(){ if (pVariantQueue) { delete pVariantQueue; }}bool bAttributeIsArray;mutable VariantQueue *pVariantQueue;};主要方法:int main(){ AttributeValueWrapper attrib;}我正在Dr Memory下运行此代码(这只是一段代码,项目相当大),Memory Dr在以下位置显示内存泄漏默认构造函数中的pVariantQueue = new VariantQueue如:  错误#46:泄漏8个直接字节+ 324个间接字节  replace_operator_new      d:\ drmemory_package \ common \ alloc_replace.c(2899):  std :: _ Allocate       ??:0  std :: allocator :: allocate      ??:0  std :: _ Wrap_alloc :: allocate      ??:0  std :: _ Deque_alloc :: _ Alloc_proxy      ??:0  std :: _ Deque_alloc :: _ Deque_alloc       ??:0  std :: deque :: deque       ??:0  AttributeValueWrapper :: AttributeValueWrapper请分享您对这个问题的想法。我也尝试过使用std::unique_ptr,但是在同一行号(相同点)上仍然会出现相同的内存泄漏:struct AttributeValueWrapper{AttributeValueWrapper() : bAttributeIsArray(false){ pVariantQueue = std::make_unique<VariantQueue>(new VariantQueue); if(!pVariantQueue) throw std::bad_alloc();}AttributeValueWrapper(const AttributeValueWrapper& a){ pVariantQueue = a.TakeOwner(); bAttributeIsArray = a.bAttributeIsArray;}AttributeValueWrapper& operator=(AttributeValueWrapper& r){ throw std::bad_exception("equal operator not supported in AttributeValueWrapper");}std::unique_ptr<VariantQueue> TakeOwner() const{ std::unique_ptr<VariantQueue> p = std::move(pVariantQueue); pVariantQueue = NULL; return p;}~AttributeValueWrapper(){}bool bAttributeIsArray;mutable std::unique_ptr<VariantQueue> pVariantQueue;};现在在发生内存泄漏pVariantQueue = std::make_unique<VariantQueue>(new VariantQueue);维奈 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您的内存泄漏很可能在析构函数中找到。队列消失后,队列中的对象会如何处理? (adsbygoogle = window.adsbygoogle || []).push({});
08-17 03:39