Jump to content

  • Log In with Google      Sign In   
  • Create Account


unique_ptr


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 deathwearer   Members   -  Reputation: 367

Like
0Likes
Like

Posted 11 December 2012 - 03:53 PM

I get the concept (Or I think I do) but I still have couple of question on the technical side of unique_ptr.

1. When you declare a unique_ptr as a class variable, you don't have to delete it in the destructor because it is not on the heap so when the class instance is deleted, the unique_ptr is deleted which trigger the real pointer delete?

2. How do you pass unique_ptr to other functions. Do you pass a ref to the unique_ptr object? Or do you pass uniqueptr.get() (Which sends the pointer to the real object)? Do you do the same if you send it to an internal method than to another class method? I personally pass the pointer as const, leaving the unique_ptr hidden. I'm wondering here what are the best practices.

3. What is the point of a unique_ptr as a class variable? I mean since the pointer has to dies when the owning instance is kill, all you have to do is to put it in the destructor. I understand that shared_ptr are more complicated since many object can own them which make the delete call tricky, but in the case of the unique_ptr, it's pretty straightforward.

4. What are the best practices regarding unique_ptr and passing the pointer to the object is hold to another class which stores it in a class variable. Is that a good practice first? The unique_ptr could be deleted and the pointer copy it hold in the other class would be invalid. Are unique_ptr meant to be pass around like that? Should i use shared_ptr in that case we're talking? So if another class stores it in a class variable the pointer would be deleted when only all the instances of these 2 classes would be deleted. Also how can you predict such cases if the good answer is to use shared_ptr. How can I plan that other people that might use my class could store my pointer internally.

Edited by deathwearer, 11 December 2012 - 04:56 PM.


Sponsor:

#2 zacaj   Members   -  Reputation: 643

Like
1Likes
Like

Posted 11 December 2012 - 04:06 PM

1. You don't have to delete it
3. because then some other function can't get a copy of your pointer and then have an invalid pointer after your class is destructed

#3 deathwearer   Members   -  Reputation: 367

Like
0Likes
Like

Posted 11 December 2012 - 04:53 PM

1. You don't have to delete it
3. because then some other function can't get a copy of your pointer and then have an invalid pointer after your class is destructed

if i pass the unique_ptr.get() they have a copy if my ptr. they could also store that pointer in a class variable. Added question 4 at the topic.

#4 zacaj   Members   -  Reputation: 643

Like
1Likes
Like

Posted 11 December 2012 - 05:03 PM

The idea is that you shouldn't have a second pointer referencing the same data. Another function could save unique_ptr.get(), just like they could just save a copy of a plain pointer, but having a unique_ptr makes it quite obvious that they shouldn't do that.

4. In this case shared_ptr would be a better bet. However, in general you should have some policy for each class regarding what 'rights' they have regarding the pointer you give them. If you're giving it to another class you should know whether that class is ever going to delete it or anything, and with any of your own classes that will be accessed by another person's code you should also make sure they know if you're going to be doing anything with their pointers. Generally I try to always have a policy regarding any place that a pointer is in my code about who 'owns' the pointer; whether it's the class that contains (say) the array the pointer is in, the function that created the pointer and added it to the array, etc.

#5 Khatharr   Crossbones+   -  Reputation: 2964

Like
1Likes
Like

Posted 11 December 2012 - 06:47 PM

shared_ptr is a way of making sure that the pointer is not deleted until everyone is done using it. Apart from that it more or less works like unique_ptr.

For instance, if you have an object that's acting as a primary owner for some shared_ptr contained resource and some other object needs to use the resource you can share the pointer to the other object. If the primary owner is destroyed before the 'borrower' is done then the resource will remain until the borrower finishes using it. weak_ptr can be used in cases where you want to share the pointer without causing this kind of relationship - in other words, if you want to share access but not ownership. The weak_ptr will let you know that the resource was deleted instead of stopping it from being deleted (because it will give you an error when you try to convert it to shared_ptr for use).

http://www.swageroo.com/wordpress/c11-standard-what-are-smart-pointers-good-for/
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#6 SiCrane   Moderators   -  Reputation: 9566

Like
2Likes
Like

Posted 11 December 2012 - 08:24 PM

Having std::unique_ptr member variables has additional advantages over not needing to call delete manually in the destructor. For one, it prevents the automatic generation of the copy constructor and copy assignment operators for the class meaning that you won't have to deal with the shallow copy double deletion issue that you can have with regular pointers. It also simplifies the creation of exception safe constructors. For example, if you have a class with three dynamically allocated objects, the exception safe version of the constructor might look like this with regular pointers:
Foo::Foo() {
  bar1 = new Bar1;
  try {
    bar2 = new Bar2;
    try {
      bar3 = new Bar3;
    } catch (...) {
      delete bar2;
      throw;
    }
  } catch (...) {
    delete bar1;
    throw;
  }
}
With unique_ptr member variables you can just assign them in the member initialization list and rely on the destruction order to clean things up in case of exception in allocating one of the objects.

Finally, a unique_ptr member variable is an explicit statement of ownership. You don't need to look at the implementation of the various member functions to know what ownership system is being used like you do with a regular pointer.

#7 deathwearer   Members   -  Reputation: 367

Like
0Likes
Like

Posted 12 December 2012 - 09:39 AM

Let's say I have three classes.

1. Player
2. GameScene
3. CameraController

The GameScene create the Player and the CameraController. Both are unique_ptr in the GameScene.

The CameraController needs the player to get it's position everytime its updates is called to center the camera on him. What I did was to receive a player pointer in the CameraController constructor and store it.

This scenario is simple enough to say, why don't you pass the pointer to the player each time you call your update method on CameraController. But for the sake of understanding the concept and good practices of unique_prt let's pretend there are other routine methods being call internally which require the player.

Should I have created a shared_ptr and pass the shared_ptr to the CameraController? So the ptr will be deleted when both the GameScene and CameraController get deleted?

#8 SiCrane   Moderators   -  Reputation: 9566

Like
1Likes
Like

Posted 12 December 2012 - 11:47 AM

Unfortunately there isn't any sort of general rule that can be used. Depending on the exact situation the best answer might be store the Player object in shared_ptrs, pass around a pointer every time you need to call a function on CamerController, store the Player as part of CameraController and have GameScene access it through the CameraController object, or something else entirely.

#9 Khatharr   Crossbones+   -  Reputation: 2964

Like
1Likes
Like

Posted 12 December 2012 - 06:21 PM

Should I have created a shared_ptr and pass the shared_ptr to the CameraController? So the ptr will be deleted when both the GameScene and CameraController get deleted?


If there's a chance that the GameScene could be destroyed during the time when CameraController is executing then shared_ptr could save you from suddenly having an invalid pointer. If the pointer value of player is not something that will change during the lifetime of GameScene then you could store the player in a shared_ptr and give CameraController a weak_ptr (so you don't have to pass the pointer for every update).
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#10 sednihp   Members   -  Reputation: 241

Like
0Likes
Like

Posted 15 December 2012 - 03:00 PM

I've been reading around smart pointers a bit recently after I finally realised that they're actually implemented in VS 2012 and came across these articles at MSDN:

Smart pointers
Unique pointers
Shared pointers
Weak pointers

They all give good pointers on how to use the various different versions and have example code to go from as well. I've bookmarked all 4 pages for reference in the future.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS