Personally I would support addition of boost::shared_ptr (and perhaps  
boost::intrusive_ptr) to our code base.

However, I would like to limit its use to situations where it is  
highly useful. It would be easy to get into a habit of using it  
everywhere, and I think this would create more confusion for new  
developers. The config class is a good example of somewhere we could  
use it though.

David

Quoting Pauli <[EMAIL PROTECTED]>:

> Hi!
>
> Adding boost smart_ptr library to wesnoth would help a managing
> dynamically allocated memory. I have plan to optimize config class with
> shared_ptr in 1.5. Shared_ptr adds reference counting to any pointer. It
> provides very simple interface to eliminate memory leaks.
>
> Any objections to add it now? I want to use it in server side to make
> sure that there is minimal danger for memory leaks so 1.4 server would
> be more stable with increasing user numbers.
>
> How to use shared_ptr:
> // NOTE: Reset has same effect as code execution going outside pointers
> scope. In practice you need rarely call reset. (but we have some palces
> where it would be practical)
> typedef boost::shared_ptr<config> configPtr;
>
> // Allways use named variable for shared_ptr that holds pointer! if not
> using one it has danger of memory leaks because C++ doesn't define code
> excecution order allways.
> configPtr cfg(new config(...));
>
> if(cfg) // Test if we have valid pointer
> {
>     cfg->add_child(...);
> }
>
> // create copy of pointer
> configPtr cfg_copy(cfg);
>
> // Now we want to "delete" original pointer
> // But because we have 2 pointers to config object
> // It isn't destroyed
> cfg->reset();
>
> if (!cfg)
> {
>    // cfg not valid pointer so we assign new config object to it
>    // We also define custom deleter for this. (default uses delete operator)
>   class deleter  {
> public:
> // operator() is called when object should be destroied
>   operator ()(config *p) { if (p) destroy_config(p));}
> }
>    cfg = configPtr(new config(...), deleter());
> }
>
> // You need raw pointer for SDL? No problem! get method gives raw
> pointer but remember to keep shared_ptr object untill raw pointer isn't
> needed any more.
> SDL_DoFancyStuff(cfg.get());
>
> // now delete copy pointer which destroies config object
> cfg_copy->reset();
>
>
> If you need cyclic pointers between objects you have to se weak_ptr
> instead of shared_ptr. weak_ptr is like observer who doesn't own
> shared_ptr object :)
>
> There is good documentations in http://www.boost.org/libs/smart_ptr/
>
> Pauli
>
>
> _______________________________________________
> Wesnoth-dev mailing list
> Wesnoth-dev@gna.org
> https://mail.gna.org/listinfo/wesnoth-dev
>




_______________________________________________
Wesnoth-dev mailing list
Wesnoth-dev@gna.org
https://mail.gna.org/listinfo/wesnoth-dev

Reply via email to