
Hi All! I am using a third party library where I enqueue a series of events. Each of these events allows the passing of a user context void*. I am attempting to pass the address of a boost::shared_ptr as my context. typedef boost::shared_ptr<CBuffer> CBufferPtr; for (i = 0; i < PREQUEUESIZE; i++) { handle = RegisterBuffer(buffer->m_pBCAMBuffer, m_AOISize); QueueBuffer( handle, &buffer ); // The second parameter is a void* user context data } In the above code "buffer" is my shared_ptr. Later, in my event handler I have the following: CBufferPtr buffer = static_cast<CBufferPtr>(result.Context()); This does not compile due to the following error in VS2005: 1>c:\boost\include\boost-1_33_1\boost\shared_ptr.hpp(125) : error C2440: 'initializing' : cannot convert from 'const void *' to 'CBuffer *' 1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast 1> d:\sourcecode\test.cpp(1572) : see reference to function template instantiation 'boost::shared_ptr<T>::shared_ptr<const void>(Y *)' being compiled 1> with 1> [ 1> T=CBuffer, 1> Y=const void 1> ] The problem seems to be that the third party library call to result.Context() is returing a const void*. It also seems to me that my handler code is actually executing the overloaded operator=, and can never actually cast from a void* to a shared_ptr<T>. What can I do to successfully cast a void* to a shared_ptr<T>? Thanks, Dave

Dave Nay wrote:
Hi All!
I am using a third party library where I enqueue a series of events. Each of these events allows the passing of a user context void*. I am attempting to pass the address of a boost::shared_ptr as my context.
typedef boost::shared_ptr<CBuffer> CBufferPtr;
for (i = 0; i < PREQUEUESIZE; i++) { handle = RegisterBuffer(buffer->m_pBCAMBuffer, m_AOISize); QueueBuffer( handle, &buffer ); // The second parameter is a void* user context data }
In the above code "buffer" is my shared_ptr.
If buffer is of type CBufferPtr here, you pass a pointer to it to the library, an entity of type CBufferPtr*. This implies that your 'buffer' variable will be alive until the third-party library is done with it. Will it?
Later, in my event handler I have the following:
CBufferPtr buffer = static_cast<CBufferPtr>(result.Context());
Here you try to cast the context (logically of type CBufferPtr*, physically
of type void*) to CBufferPtr. Nothing good can come from this. :-) The
opposite of the above should be
CBufferPtr buffer = *static_cast
participants (3)
-
Dave Nay
-
Gottlob Frege
-
Peter Dimov