• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
deathwearer

unique_ptr

9 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
[quote name='zacaj' timestamp='1355263599' post='5009558']
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
[/quote]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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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).

[url="http://www.swageroo.com/wordpress/c11-standard-what-are-smart-pointers-good-for/"]http://www.swageroo.com/wordpress/c11-standard-what-are-smart-pointers-good-for/[/url]
1

Share this post


Link to post
Share on other sites
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:
[code]
Foo::Foo() {
bar1 = new Bar1;
try {
bar2 = new Bar2;
try {
bar3 = new Bar3;
} catch (...) {
delete bar2;
throw;
}
} catch (...) {
delete bar1;
throw;
}
}
[/code]
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.
2

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[quote name='deathwearer' timestamp='1355326772' post='5009853']
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?
[/quote]

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).
1

Share this post


Link to post
Share on other sites
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:

[url="http://msdn.microsoft.com/en-us/library/vstudio/hh279674.aspx"]Smart pointers[/url]
[url="http://msdn.microsoft.com/en-us/library/vstudio/hh279676.aspx"]Unique pointers[/url]
[url="http://msdn.microsoft.com/en-us/library/vstudio/hh279669.aspx"]Shared pointers[/url]
[url="http://msdn.microsoft.com/en-us/library/vstudio/hh279672.aspx"]Weak pointers[/url]

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0