私は、このように独自のメモリ マネージャーで boost shared_ptr を使用しています (例を削除しました。エラーがないことを願っています)。
class MemoryManager
{
public:
/** Allocate some memory.*/
inline void* allocate(size_t nbytes)
{
return malloc(nbytes);
}
/** Remove memory agian.*/
inline void deallocate(void* p)
{
free(p);
}
};
MemoryManager globalMM;
// New operators
inline void* operator new(size_t nbytes, ogl2d::MemoryManagerImpl& mm)
{
return globalMM.allocate(nbytes);
}
// Corresponding delete operators
inline void operator delete(void *p, ogl2d::MemoryManagerImpl& mm)
{
globalMM.deallocate(p);
}
/** Class for smart pointers, to ensure
* correct deletion by the memory manger.*/
class Deleter
{
public:
void operator()(void *p) {
globalMM.deallocate(p);
}
};
そして、私はそれを次のように使用しています:
shared_ptr<Object>(new(globalMM) Object, Deleter);
しかし、今、私は気づいています。shared_ptr が onject を削除すると、Deleter::operator() が呼び出され、オブジェクトが削除されます。しかし、デストラクタは呼び出されません...
どうすればこれを変更できますか?