Re: question: shared pointers with custom deleters
Am 28.08.2011 20:17, schrieb Andrew:
I am writing a uuid class. It uses the native WIN32 API on windoze and
uses a C uuid library otherwise. The uuid library header file fwd
declares uuid_t and all the functions take a pointer to uuid_t. Hence
uuid_t does not need to be defined, a fwd-declare is enough. The
details are in the C implementation, the header file is hiding the
implementation details (quite right too).
The problem I have is that my uuid objects need to exist inside STL
containers so they will be copied. I need to ensure that the memory
allocated by the uuid library is only deallocated once. So I thought I
would be able to make the uuid_t pointer a shared pointer with a
custom deleter. The custom deleter uses uuid_destroy(uuid_t*).
At this point I'm not sure whether I understand your approach correctly.
What does "make the uuid_t pointer a shared pointer" mean precisely?
However, I am getting a massive compilation error message during the
parsing of some custom deleter header file that is getting pulled in
somehow. The error message makes me think that the use of a custom
deleter requires that the type be fully known. The error message
includes this:
/usr/local/boost/include/boost/checked_delete.hpp:32: error: invalid
application of 'sizeof' to incomplete type 'uuid_t'
I know this is boost but surely the same applies for the equivalent
smart pointers in TR1.
Can anyone comment on this please?
You need to provide at least the minimum code necessary to reproduce the
problem. I think this should work, at least if I understand your idea
correctly. E.g. using the current boost library 1.47.0 the following
code compiles successfully with gcc 4.7.0:
extern "C" {
struct uuid_t;
uuid_t* uuid_create();
void uuid_destroy(uuid_t*);
}
#include <boost/shared_ptr.hpp>
int main()
{
boost::shared_ptr<uuid_t> pu(uuid_create(), uuid_destroy);
}
Looking at the boost documentation (or for comparison: The new C++11
standard specification of std::shared_ptr), there is no requirement that
uuid_t must be complete, when shared_ptr<uuid_t> is created with the
provided deleter.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]