不完全类型内存泄漏?

编程入门 行业动态 更新时间:2024-10-23 18:21:07
本文介绍了不完全类型内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Microsoft Visual Studio 2008给我以下警告:

Microsoft Visual Studio 2008 is giving me the following warning:

警告C4150:删除指向不完整类型GLCM :: Component的指针;没有析构函数调用

warning C4150: deletion of pointer to incomplete type 'GLCM::Component'; no destructor called

这可能是因为我已经定义了Handles在几个地方转发声明的类型,所以现在Handle类声称它不会调用析构函数给定对象。

This is probably because I have defined Handles to forward declared types in several places, so now the Handle class is claiming it won't call the destructor on the given object.

我有VLD运行,我没有看到任何泄漏。这真的不是为这个对象调用析构函数,或者这是一个可能不调用对象的析构函数警告?

I have VLD running and I'm not seeing any leaks. Is this literally not calling the destructor for this object or is this a "may not call destructor for object" warning?

还有另一个内存泄漏问题从我,哈哈。 / p>

Yet another memory leak question from me, haha.

推荐答案

使用Pimpl时经常发生,所以我将集中在那里的解决方案:

It often happen when using Pimpl, so I'll focus on the solution there:

class FooImpl; class Foo { public: // stuff private: Pimpl<FooImpl> m_impl; };

这里的问题是,除非你声明一个析构函数,否则它会自动生成,编译器。但是当然,编译器不知道 FooImpl 的完整类型。

The problem here is that unless you declare a destructor, it will be automatically generated, inline, by the compiler. But of course, the compiler will have no idea of the complete type of FooImpl there.

显式定义析构函数,即使是空的,并将定义放置在 FooImpl 的完整类型的某个位置。

You thus have to explicitly define the destructor, even if empty, and put the definition somewhere where the complete type of FooImpl is visible.

// cpp file class FooImpl { }; Foo::~Foo() {} // Empty, but now correctly generated // because FooImpl complete at this point.

此外,如果像我一样,您定义了 Pimpl 类非常聪明(关于构造,复制和赋值),那么这些也需要在.cpp文件中定义。

Also, if like me you defined your Pimpl class to be pretty smart (regarding construction, copy and assignment), then those will also need to be defined in the .cpp file.

这真的是一个麻烦,那么你很好地封装了你的实现细节,所以我想这是值得的。

It's really a hassle, but then you have nicely encapsulated your implementation details, so I suppose it's worth it.

更多推荐

不完全类型内存泄漏?

本文发布于:2023-11-27 08:58:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不完全   内存   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!