|
I have found it to be ultra helpful. Memory leaks are much easier to fix.
The over head is quite small as far as I can see. This pointer class is taken from boost, so you can see the actual implementation there, if you like.
>You suggest that replacing all my ~15 or so pointers with shared_ptr will fix this memory leak?
It’s not going to hurt and it may fix it or help you find out where it is. It may not even be a leak.
These are in a header:
// destructor templates for smart_ptr
template <class _Type>
class releaser
{ public:
void operator()(_Type *p)
{
p->Release();
}
};
template <class _Type>
class deleter
{ public:
void operator()(_Type *p)
{
delete p;
}
};
#define smart_ptr std::tr1::shared_ptr
I define the pointer like this:
// class I want to allocate
class FBXMesh
{
... };
typedef smart_ptr<FBXMesh> spFBXMesh;
Then the allocation is:
spFBXMesh spMesh(new FBXMesh, deleter<FBXMesh>()); // pointer definition
// or
spFBXMesh spMesh;
// reassign a pointer (also frees up a previous assignment
spMesh.reset(new FBXMesh, deleter<FBXMesh>()); // pointer definition
When spMesh goes out of scope, the deleter function is called.
Author: Doug Rogers
|