The problem is in your destructor. You repeat the declaration
of GlobalFn
, rather than call it. It should be:
~HandleWrap() { (*GlobalFn)( data ); }
Also, do you want to make this class copyable, movable or neither? If neither, you should take steps to prevent any of the relevant compiler generated defaults; otherwise, you'll need to provide the corresponding constructors (and possibly assignment operators). If copyable, you'll also need some sort of counter, shared between all of the copies, so that only the last destructor frees the handle. For movable (probably the best solution, if you can be sure of having C++11), you'll need a move constructor, which does something to ensure that the destructor of the moved from object is a no-op.