[wm4] LibGraphics/ObjectSystem/Wm4SmartPtr
2010-01-29 23:49:57| 分类:
3d_WildMagic
| 标签:
|举报
|字号大中小 订阅
继承自 Wm4::Object 的 class,会带有 ref count,配合 Wm4::Pointer 使用,则可让你不关心何时回收内存。使用时,只负责 new 即可。
具体实现也很传统, Wm4::Pointer 构造时加引用、析构时减引用、引用为0删除之。
----------------------------------------
#include <Wm4Object.h>
#include <stdio.h>
class MyFoo : public Wm4::Object
{
public:
MyFoo() { printf("ctor = %p\n", this); }
~MyFoo() { printf("dtor = %p\n", this); }
};
int main()
{
Wm4::Pointer<MyFoo> p1 = WM4_NEW MyFoo;
printf("ref 1 = %d\n", p1->GetReferences());
Wm4::Pointer<MyFoo> p2 = p1;
printf("ref 2 = %d\n", p1->GetReferences());
return 0;
}
----------------------------------------
$ ./a.out
ctor = 0x28f0a120
ref 1 = 1
ref 2 = 2
dtor = 0x28f0a120
----------------------------------------
除此之外,Wm4::Object 还将所有创建出来的 Object 的子类的 instance,全部都索引起来,可以随时 dump 下来查证问题。
static THashTable<unsigned int,Object*>* InUse;
static void PrintInUse (const char* acFilename, const char* acMessage);
评论这张
转发至微博
转发至微博
评论