Ahhh well. I thought I was doing so well, but then I ran the code below 
through valgrind. I have now discovered that I can't hide the parent class 
destructor :-(

I guess I'd better go back to real work.

Angus

==31335== Invalid free() / delete / delete[]
==31335==    at 0x4015E825: __builtin_delete (vg_clientfuncs.c:196)
==31335==    by 0x4015E844: operator delete(void*) (vg_clientfuncs.c:205)
==31335==    by 0x80486CA: void boost::checked_delete<int>(int*)
==31335==    by 0x80486AA: boost::scoped_ptr<int>::~scoped_ptr()
==31335==    Address 0x41127024 is 0 bytes inside a block of size 4 free'd
==31335==    at 0x4015E79D: free (vg_clientfuncs.c:182)
==31335==    by 0x8048647: lyx::custom_scoped_ptr<int>::~custom_scoped_ptr()


#include <cstdlib>
#include <boost/scoped_ptr.hpp>

namespace lyx {

template <typename T>
class custom_scoped_ptr : public boost::scoped_ptr<T> {
public:
        explicit custom_scoped_ptr(T * p, void (* d)(void *))
                : boost::scoped_ptr<T>(p), destroyer(d) {}
        /// This destructor HIDES that of scoped_ptr.
        ~custom_scoped_ptr() { destroyer(get()); }
private:
        void (* destroyer)(void *);
};

} // namespace lyx


int main ()
{
        lyx::custom_scoped_ptr<int> ptr((int *)malloc(sizeof(int)), free);
        *ptr = 1;
        printf("*ptr = %d\n", *ptr);
        return 0;
}


Reply via email to