[Qt-interest] QSharedPointer on opaque data structures

Thiago Macieira thiago.macieira at trolltech.com
Sat Aug 22 19:18:25 CEST 2009


Em Sábado 22. Agosto 2009, às 19.00.05, Daniel Franke escreveu:
> On Saturday 22 August 2009 18:36:47 you wrote:
> > 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.
>
> If I do this ...
>
> --- qsharedpointer_impl.h.orig  2009-08-22 18:53:11.000000000 +0200
> +++ qsharedpointer_impl.h       2009-08-22 18:53:21.000000000 +0200
> @@ -121,7 +121,7 @@
>          inline T *operator->() const { return data(); }
>
>      protected:
> -        inline Basic() : value(0 * sizeof(T)) { }
> +        inline Basic() : value(0) { }
>          // ~Basic();
>
>          inline void verifyReconstruction(const T *ptr)
>
>
> ... the quoted error vanishes and the code compiles, but the warnings
> are still there.
>
> Would someone care to explain if the sizeof() I removed is significant
> here? (I'd be mightily surprised if yes, but hey, one never knows ...)

It's removed in 4.6.

-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Product Manager - Nokia, Qt Development Frameworks
     Sandakerveien 116, NO-0402 Oslo, Norway

Qt Developer Days 2009 | Registration Now Open!
Munich, Germany: Oct 12 - 14     San Francisco, California: Nov 2 - 4
      http://qt.nokia.com/qtdevdays2009




More information about the Qt-interest-old mailing list