问题
I have a main process that owns a Model object. The purpose of this is to share a Model between various objects that performs tasks and modifies the same Model.
Right now, I create a unique_ptr to this object and pass a reference to that unique_ptr to other objects to perform certain tasks.
The tasks all complete correctly, but something weird happens when it tries to call the destructor on the unique_ptr. It shows:
RAW: memory allocation bug: object at 0xd0ebdfb2b8 has never been allocated
Question:
- Why is this error happening?
- Is unique_ptr the right way to tackle this problem?
Thanks
回答1:
1. Why is this error happening?
Without seeing your code it is impossible to answer why this is happening.
2. Is unique_ptr the right way to tackle this problem?
According to Straustrup and Sutter's Core Guidlines
R.30: Take smart pointers as parameters only to explicitly express lifetime semantics
Reason: Accepting a smart pointer to a
widget
is wrong if the function just needs thewidget
itself. It should be able to accept anywidget
object, not just ones whose lifetimes are managed by a particular kind of smart pointer. A function that does not manipulate lifetime should take raw pointers or references instead.
You should only be passing a std::unique_ptr
to a function if that function may need to alter its ownership. Otherwise you should pass the raw pointer or a reference to the object that the unique_ptr
is managing.
回答2:
I'd need to see a code example to give you a specific solution or feedback.
However, this line:
Right now, I create a unique_ptr to this object and pass a reference to that unique_ptr to other objects to perform certain tasks.
is very suspicious to me. It's almost always incorrect to pass the reference to your unique_ptr
(or other owning data structure) around. You should favor a simple reference to the underlying object; that is, you should replace all instances of std_unqiue_ptr<Model>&
with Model&
.
回答3:
When I read it:
Right now, I create a unique_ptr to this object and pass a reference to that unique_ptr to other objects to perform certain tasks.
I understand something like
std::unique_ptr<type> u( new type() );
std::unique_ptr<type> w = u;
But the principe of the unique_ptr
is to be handled only one time. This throw an error because the copy-contructor of a unique_ptr
-wrapped object is disabled.
But without an example, dificult to answer :)
回答4:
Contrary to some answers here, there is nothing wrong with passing managing entities (shared_ptr, unique_ptr) by const reference. In fact, passing by reference might be the only way in some cases with unique_ptr and preferred with shared_ptr. Example:
void foo(const std::shared_ptr<obj_t>/*&*/ obj) { obj->func(); }
Without the reference, there would be an unncessary reference counter increment/decrement, causing performance delay. No, someone would argue that there is no need to use shared_ptr in the foo() to begin with, and I would agree, but some development environments I've seen are really strict about 'no naked pointers in our code!', and arguing them wrong might cost you your job. In this case, passing by reference is the solution.
来源:https://stackoverflow.com/questions/32873299/unique-ptr-destructor-saying-object-has-never-been-allocated