56769

unique_ptr with an API that expects raw pointers?

Question:

After about 10 years of using managed memory and functional languages, I'm finally coming home to C++, and smart pointers are confusing the heck out of me. Half of the documentation out there is still regarding the deprecated auto_ptr.

I'm trying to implement this fairly straightforward Bullet <a href="http://bulletphysics.org/mediawiki-1.5.8/index.php/Hello_World#Initial_Program" rel="nofollow">"hello world"</a> program:

int _tmain(int argc, _TCHAR* argv[]) { auto bp = unique_ptr<btBroadphaseInterface>(new btDbvtBroadphase); auto cc = unique_ptr<btDefaultCollisionConfiguration>(new btDefaultCollisionConfiguration); auto disp = unique_ptr<btDispatcher>(new btCollisionDispatcher(cc)); }

The btCollisionDispatcher constructor wants a btCollisionConfiguration*, but I'm giving it a unique_ptr to one instead.

What do I normally want to do in this case? If there's a way to "de-smart" the pointer, something tells me that unique_ptr isn't the right smart pointer to use.

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/4Noy6.png" data-original="https://i.stack.imgur.com/4Noy6.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

C++ was my language of choice before I moved to other things. It's a little shocking coming back and seeing that all the patterns and practices have completely changed.

Answer1:

There is a get() member function that gives you the raw pointer that is held by the unique_ptr. This does not cause the unique_ptr to relinquish the ownership, though, so proper cleanup will still happen (careful with storing that raw pointer!).

There is also a release() member function, which relinquishes ownership. This means that you're back on dumb pointer land and cleanup is all your responsibility.

I can't fathom why the code is using new in the first place and not <a href="https://stackoverflow.com/questions/8839943/why-does-the-use-of-new-cause-memory-leaks-in-c/8840302#8840302" rel="nofollow">just using automatic storage objects</a>, but I'm going to pretend there is a reason...

Answer2:

The <a href="http://msdn.microsoft.com/en-us/library/ee475590.aspx" rel="nofollow">get</a> member function returns the underling pointer and is fine to use with existing code as long as that code doesn't manage the memory you pass in.

Recommend

  • Address of pointer “this” changed unexpectedly inside a pointer to member function call
  • C++ explicit return type template specialisation
  • What initialization should be made prior to calling InternetGetProxyInfo()?
  • visual studio 2013 static code analysis - how reliable is it?
  • Why does referencing break when done through pointers
  • Pantheios write to extenal file
  • Converting _TCHAR* to char*
  • WTSQuerySessionInformation returning empty strings
  • Allocating class member with std::shared_ptr
  • Does _control87() also set the SSE MXCSR Control Register?
  • Can std::cout work with UTF-8 on Windows?
  • Create a sticky footer in a responsive manner
  • Comparative genomics: how to compare ranges of sequences
  • wrap content in JavaFX
  • How can I add a class with jquery once dynamic loading is finished?
  • std::list erase incompatible iterator
  • Python kludge to read UCS-2 (UTF-16?) as ASCII
  • Using QProcess.finished() in Python 3 and PyQt
  • Writing an RDA to CSV in R
  • OPENCV : CUDA context initialization for different methods
  • Pass Arbitrary Sized 2 Dimension Array
  • What is lua_len() alternative in Lua 5.1?
  • std::system Exception when instantiating a singleton object
  • Call a macro with parameters : Python win32com API
  • Get the App path without the app name at the end of the app
  • Getting short path in python
  • How to read piped content in C?
  • Hibernate to update table schema
  • C: Incompatible pointer type initializing
  • Jackson Parser: ignore deserializing for type mismatch
  • Change multiple background-images with jQuery
  • Trying to switch camera back to front but getting exception
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal