简体   繁体   English

我们应该在Qt中使用哪种内存管理方法?

[英]Which memory management method should we use in Qt?

I'm quite new to programming as I'm still learning how to code efficiently so my question may seem a little bit strange. 我对编程很陌生,因为我还在学习如何高效编码,所以我的问题看起来有点奇怪。

As I'm learning how to code with Qt, I know that Qt has a memory management method that the parent deletes all its children when it itself was deleted, and then there's QSharedPointer and so many other smart pointer thing (or you may mention boost::shared_ptr ). 当我正在学习如何使用Qt进行编码时,我知道Qt有一个内存管理方法,当父QSharedPointer被删除时,父进程会删除它的所有子QSharedPointer ,然后是QSharedPointer和其他许多智能指针(或者你可能会提到boost::shared_ptr )。

I understand how exactly both methods work but my question is that when it comes to the design of the whole system structure in Qt, should i make most of my class a subclass of QObject and hand over the ownership to QT to do the memory management, or simply using boost::shared_ptr or QSharedPointer thing to avoid potential memory leaks ? 我理解两种方法究竟是如何工作的,但我的问题是,当涉及到Qt中整个系统结构的设计时,我是否应该将我的大部分类作为QObject的子类并将所有权移交给QT来进行内存管理,或者只是使用boost::shared_ptrQSharedPointer来避免潜在的内存泄漏?

As there's a lot of problem to take into account if we adopt both techniques because it may cause double-delete problem. 因为如果我们采用这两种技术会有很多问题需要考虑,因为它可能导致双删除问题。

So which technique has better performance or which is better in the design? 那么哪种技术具有更好的性能,哪种技术在设计上更好?

As you know, Qt has a model for which: 如您所知,Qt有一个模型,其中:

QObjects organize themselves in object trees. QObjects在对象树中组织自己。 When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is. 当您使用另一个对象作为父对象创建QObject时,它将添加到父对象的children()列表中,并在父对象时删除。

Moreover: 此外:

You can also delete child objects yourself, and they will remove themselves from their parents. 您也可以自己删除子对象,并将自己从父母身上删除。

Because of that, as long as you use that model, you won't have problems of double frees. 因此,只要您使用该模型,就不会出现双重释放的问题。

That said, a common approach I've seen and used (but be aware that there was ever a reason to do that) is to create two layers, the former as a purely Qt-based one and the latter completely Qt-unaware. 也就是说,我见过和使用的一种常见方法(但要注意有理由这样做)是创建两个层,前者是纯粹的基于Qt的层,后者完全是Qt-unware。 Of course, it requires a thin layer that translate pieces of information back and forth. 当然,它需要一个薄层来回传递信息。
In such a model, it is reasonable to see both the approaches applied, never mixed and correctly working. 在这样的模型中,可以合理地看到两种方法都适用,从不混合和正确工作。

So, which is the best one? 那么哪个是最好的? It depends on your target. 这取决于你的目标。

I've used a mixed approach in cases where the underlying layer was meant as a standalone codebase on top of which I was able to create an interface using my preferred library, but also I wanted to be free to switch to whatever library for the UI. 我已经使用了一种混合方法,其中底层是一个独立的代码库,我可以使用我喜欢的库创建一个接口,但我也想自由切换到UI的任何库。
If this is not the case and your project is a purely Qt-based one, there is no reason not to base everything on the model on which Qt itself is based. 如果情况并非如此,并且您的项目纯粹是基于Qt的项目,则没有理由不将所有内容都基于Qt本身所基于的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM