[Qt-interest] QSharedPointer on opaque data structures

Daniel Franke franke.daniel at gmail.com
Sat Aug 22 18:36:47 CEST 2009


Hi all.

I have a pointer to an opaque data structure, which generally can be used as:

	struct opaque;
	struct opaque *ptr = opaque_structure_create();
	// deal with 'ptr'
	opaque_structure_free(ptr);

Now I'd like to use 'ptr' as a shared pointer where the last user calls 
'opaque_structure_free'. From the docs of QSharedPointer (4.5.2), I assumed 
that this should work:

	QSharedPointer<struct opaque> sharedPtr(opaque_structure_create(),
                                                opaque_structure_free);
	// use shared pointer, pass around copies, 
	// last user calls opaque_structure_free

However, it does not compile:

/usr/include/qt4/QtCore/qsharedpointer_impl.h:124: error: invalid application 
of 'sizeof' to incomplete type 'struct opaque'
/usr/include/qt4/QtCore/qsharedpointer_impl.h: In member function 'void 
QtSharedPointer::ExternalRefCount<T>::internalDestroy() [with T = struct 
opaque]':
/usr/include/qt4/QtCore/qsharedpointer_impl.h:195:   instantiated from 'bool 
QtSharedPointer::ExternalRefCount<T>::deref() [with T = struct opaque]'
/usr/include/qt4/QtCore/qsharedpointer_impl.h:217:   instantiated from 
'QtSharedPointer::ExternalRefCount<T>::~ExternalRefCount() [with T = struct 
opaque]'
/usr/include/qt4/QtCore/qsharedpointer_impl.h:273:   instantiated from here
/usr/include/qt4/QtCore/qsharedpointer_impl.h:231: warning: possible problem 
detected in invocation of delete operator:
/usr/include/qt4/QtCore/qsharedpointer_impl.h:231: warning: invalid use of 
incomplete type 'struct opaque'
[localfile].h:35: warning: forward declaration of 'struct opaque'
/usr/include/qt4/QtCore/qsharedpointer_impl.h:231: note: neither the 
destructor nor the class-specific operator delete will be called, even if they 
are declared when the class is defined.

Why does QSharedPointer need to know what the pointer it holds points to?
Any ideas, pointers, workarounds or solutions would be appreciated!

Thanks

	Daniel




More information about the Qt-interest-old mailing list