Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

make shared_ptr not use delete

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 0.73 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. boost::shared_ptr<T> ptr( new T, std::mem_fun_ref(&T::deleteMe) );
  2. boost::shared_ptr<S> ptr( new S, std::ptr_fun(lib_freeXYZ) );
  3.        
  4. void Deleter( T* ptr);
  5.        
  6. boost::shared_ptr<T> ptrToT( new T, Deleter );
  7.        
  8. void Deleter( T* ptr);
  9.    {
  10.         ptr->deleteMe();
  11.         // And make sure YOU ACTUALLY DELETE (or do whatever else you need to
  12.         // do to release the resource)
  13.         delete ptr;
  14.    }
  15.        
  16. void intrusive_ptr_add_ref(foo *f) {
  17.     lib_add_ref(f);
  18. }
  19.  
  20. void intrusive_ptr_release(foo *f) {
  21.     if(lib_dec_ref(f) == 0)
  22.         lib_free(f);
  23. }
  24.        
  25. intrusive_ptr<foo> f(lib_alloc());
  26.  
  27. // can wrap raw pointers too, which already may be referenced somewhere else
  28. foo *p = get_foo_from_somewhere();
  29. function_taking_intrusive_ptr(p);