boost::unique_ptr and problems with deletor argument

C

Christopher

Quite awhile back it was advised to me to use a boost::unique_ptr to
return an allocated object that I am obtaining from a COM library and
passing back to the caller, where the caller will be the owner. All I
need from standard C++ is the ability to store the pointer and provide
my custom deletion, because I must call Release() on the object.

I finally got around to trying it (as opposed to a shared_ptr) and my
compiler is complaining that I have an invalid template argument for
'D'

How do I tell it," I just want you to call the object's Release()
method? "

Here is my typedef
typdef boost::interprocess:unique_ptr<ID3D10Device, boost::mem_fn
(&ID3D10Device::Release)> ID3D10Device_UniquePtr;

with ID3D10Device being the interface I am obtaining from the COM
library upon which I must call Release()
 
S

SG

Quite awhile back it was advised to me to use a boost::unique_ptr to
[...]

my custom deletion, because I must call Release() on the object.

I finally got around to trying it (as opposed to a shared_ptr) and my
compiler is complaining that I have an invalid template argument for
'D'

I'm not familiar with boost's "unique_ptr-emulation" but if it is
comparable to the proposed std::unique_ptr for C++0x then the
deleter's type needs to be statically known.
How do I tell it," I just want you to call the object's Release()
method? "

Here is my typedef
typdef boost::interprocess:unique_ptr<ID3D10Device, boost::mem_fn
(&ID3D10Device::Release)> ID3D10Device_UniquePtr;

with ID3D10Device being the interface I am obtaining from the COM
library upon which I must call Release()

"boost::mem_fn" is a function that returns a function object. You
can't use this expression as a template's type parameter. What you
need here is the TYPE of that function object. Maybe you can get this
to work with Boost.TypeOf.

Alternativly, you might want to use a "polymorphic" deleter so you
don't have to mention the exact deleter type. This is what shared_ptr
already does for you automatically and internally. If you want a
similar behaviour you could use boost::function for this.

typedef unique_ptr<ID3D10Device,
function said:
> ID3D10Device_UniquePtr;

Note: Moving these kinds of pointers may include cloning a heap-
allocated deleter object.

I guess shared_ptr uses polymorphic deleters by default because it
needs to allocate some heap memory anyways for the reference
counters. The reference counters can be combined with the deleter
into a single object.

HTH,
SG
 
C

Christopher

Ok, I got the deletor.
Now I don't understand how to assign it.

If the callers wants to obtain and use the unique_ptr, I can't assing
it, copy construct it, or think of any other way to use the return
value of the function creating the unique_ptr.

ID3D10Device_UniquePtr ptr = CreateDevice();

Fails compilation saying the member is assignment operator is private.
As does Copy construction.
It is mentioned in the docs, but what I don't get is what
"MoveAssignable" means. No manner of searching on Boost's site or on
Google has provided a defintion for "MoveAssignable"

I did however stumble across something called std::move, which
apparently isn't supported by my compiler and may not be until after
2010, since it appears to be part of the new standard in the works.

So, how do I claim ownership of this unique_ptr?
 
T

Thomas J. Gritzan

Christopher said:
Quite awhile back it was advised to me to use a boost::unique_ptr to
return an allocated object that I am obtaining from a COM library and
passing back to the caller, where the caller will be the owner. All I
need from standard C++ is the ability to store the pointer and provide
my custom deletion, because I must call Release() on the object.

I finally got around to trying it (as opposed to a shared_ptr) and my
compiler is complaining that I have an invalid template argument for
'D'

How do I tell it," I just want you to call the object's Release()
method? "

Why don't you use CComPtr from ATL or boost::intrusive_ptr?
It's a better fit for a COM object that already has AddRef() and
Release() functions.
 
B

brekehan1

Why don't you use CComPtr from ATL or boost::intrusive_ptr?
It's a better fit for a COM object that already has AddRef() and
Release() functions.


To be honest I don't even know what ATL is and I fear adding any more
third party libraries than I have to. I have a rough enough time
trying to layer over the Windows API and DirectX to use a more
standard interface. I'll read up on it a bit though.
 
T

Thomas J. Gritzan

To be honest I don't even know what ATL is and I fear adding any more
third party libraries than I have to. I have a rough enough time
trying to layer over the Windows API and DirectX to use a more
standard interface. I'll read up on it a bit though.

Active Template Library (ATL) are template classes developed by
Microsoft to simplify working with COM objects. So when you work with
DirectX, you most likely have ATL installed already.

Newsgroup: microsoft.public.vc.atl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top