|
Boost Users : |
Subject: [Boost-users] shared_ptr to stack-allocated object
From: Jason Cipriani (jason.cipriani_at_[hidden])
Date: 2008-12-04 19:03:09
I'm refactoring parts of an existing application to use shared_ptrs
instead of raw pointers. What is the best way to deal with code like
this (please note in this situation passing a pointer to a
stack-allocated object to function is acceptable and safe):
void function (shared_ptr<Object> ptr) { ... }
void function2 () {
Object o;
function(&o); // <--- need to change this
}
It seems I have two choices, I am not sure which is better, or if
there are other common techniques. I can either use a no-op delete
function:
void noop (Object *) {}
void function2 () {
Object o;
function(shared_ptr<Object>(&o, noop));
}
Or I can allocate it on the heap instead of the stack, and let
shared_ptr deal with the cleanup (this seems to be the more convenient
solution):
void function2 () {
shared_ptr<Object> o(new Object);
function(o);
}
What's the normal practice here?
Thanks,
Jason
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net