I wanted to create some smart pointers for sqlite3
using CHeapPtr
. CHeapPtr
uses the CCRTAllocator
class by default, so I figured I'd just create some custom allocator classes that inherit from CCRTAllocator
but override its Free
method, so that they use the proper clean-up functions:
class CSqlite3Allocator : public CCRTAllocator
{
public:
static void Free(_In_ sqlite3* p) throw()
{
sqlite3_close_v2(p);
}
};
class CSqlite3StmtAllocator : public CCRTAllocator
{
public:
static void Free(_In_ sqlite3_stmt* p) throw()
{
sqlite3_finalize(p);
}
};
Now I can create CHeapPtr
s like so:
CHeapPtr<sqlite3, CSqlite3Allocator> psqlite;
CHeapPtr<sqlite3_stmt, CSqlite3StmtAllocator> pstmt;
These pointers seem to work correctly, and it appears that their clean-up methods are being called. I was just wondering if this is the proper way of doing this, or if there's a more accepted way of creating custom CHeapPtr
s. Thank you for any input.