Does anybody use Smart pointers?

im not sure how memory management works on Cocos2d-x I wanted to know if anybody here uses smart pointers or regular pointers in their game ?

I use smart pointers all the time for anything that I put on the screen I.e sprites. Labels. Layers. Etc.

1 Like

Use them where it makes sense to do so. I use them throughout the code base for the app I’m working on, but I also use raw pointers too, generally as parameters to functions or return values where the value can be null, and where the receiving method is not responsible for freeing the memory the pointer is tied to.

It’s about managing ownership, making it quick to spot what section of code is responsible for the allocated memory. Since I follow the specific rules I’ve set for the usage of the pointers in my code base, it’s used consistently throughout, and can quickly tell the purpose of the pointer.

1 Like

If you working with cocos objects then use RefPtr from CCRefPtr.h but not use std::shared_ptr !
For other non-cocos objects use std::shared_ptr.

3 Likes

what would be an example of a non-cocos object ?

A non-cocos object is anything that doesn’t inherit from cocos2d::Ref. For instance, if you have your own classes that don’t inherit from cocos2d::XXXXX classes, then you may want to use std::shared_ptr or std::unique_ptr for them if you plan on using pointers.

3 Likes

ok , thanks for the clarification!