有没有办法扫描,当人们忘记调用虚拟的基类版本? [英] Is there a way to scan for when people forget to call the base class version of a virtual?

查看:106
本文介绍了有没有办法扫描,当人们忘记调用虚拟的基类版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚修复了一个内存泄漏引起的人忘记调用超类的OnUnload在它们的覆盖。超类版本释放了一些资源(和它的超类一样)。

I just fixed a memory leak caused by someone forgetting to call the superclass's OnUnload in their override of it. The superclass version frees some resources (as does its superclass).

有没有外部静态分析工具,或者至少有一些运行时技巧可以检测这个?有明显的异常(虽然这些情况是非常罕见的)。

Are there external static analysis tools, or at least some kind of runtime trick I can do to detect this? With the ability to make an exception obviously (though these cases are exceedingly rare).

更新:基于下面的答案,我需要添加特定于我们的约束设置,这是一个游戏的Wii / 360 / PS3。非常具体的引擎用于一个非常具体的应用程序。

UPDATE: Based on the answers below, I need to add constraints that are specific to our setup, which is a game for Wii/360/PS3. Very specific engine for a very specific application.


  • 我们有一个深层次的游戏对象类,但它是我们运送的设计)。深而宽,实际上。我将重做这个为下一个游戏使用Dungeon Siege风格的基于组件的系统,但在我们当前的代码库,深层次结构使得DispatchVirtual() - > onVirtual()模式难以应用。

  • We have a deep hierarchy of game object classes (a design I never agreed with, but it's the design we are shipping). Deep and wide, actually. I am going to redo this for the next game to use a Dungeon Siege-style component-based system but in our current codebase, deep hierarchies make the DispatchVirtual() -> onVirtual() pattern difficult to apply.

Destructors不会为我们的游戏对象调用,因为delete不会被调用。在世界负载期间,游戏对象进入基于添加(堆栈)分配器的池。在一个级别的结尾,我只是将堆栈指针设置回低水位标记,以便立即释放所有内容。事先,我们迭代所有对象,并调用OnUnload,以便他们可以释放所使用的任何外部资源。你可以称之为垃圾回收:核选项。所以没有析构函数。

Destructors do not get called for our game objects because delete doesn't get called. Game objects go into an add-only (stack) allocator-based pool during a world load. At the end of a level I just set the stack pointer back to the low water mark to free everything at once. In advance, we iterate all objects and call OnUnload on them so they can free any external resources they use. You might call it "garbage collection: the nuclear option". So no destructors.

即使我们可以使用基于析构函数的方法,它只会解决OnUnload或OnFree的狭窄问题,而不是OnUpdate,OnWorldMessage ,OnLoaded等。

Even if we could use a destructor-based approach it would only solve the narrow problem of an OnUnload or OnFree, but not OnUpdate, OnWorldMessage, OnLoaded, etc.

运行时解决方案很有趣,但我讨厌依靠测试来捕捉这个。最佳将是编译时模板技巧或可以使用的外部静态分析工具。

Runtime solutions are interesting but I hate relying on testing to catch this. Optimal would be either a compile-time template trick or an external static analysis tool I can use.

推荐答案

你可以使用is是在基类的析构函数声明,如果你正在寻找的约束失败。假设实例被实际销毁并且没有泄露,这将告诉你在对象被销毁的时候,如果合同被正确遵循。

A runtime "trick" you could use is to assert in the destructor of the base class if the constraint you are looking for has failed. Assuming the instance is actually destroyed and not leaked, this will tell you at the time the object is destroyed if the contract was correctly followed.

这篇关于有没有办法扫描,当人们忘记调用虚拟的基类版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆