- changed status to open
- assigned issue to
- removed comment
Piraha smart_ptr can't handle self assignment
An assignment of the form
void foo(smart_ptr<T> x) {
x = x;
}
will not work, since operator= assumes that LHS and RHS are different objects. The usual remedy is to add an if statement, doing nothing for self assignment.
Keyword:
Comments (9)
-
-
- removed comment
Just curious: why would you need this in a parameter file?
-
reporter - removed comment
This is not about parameter files, this is about C++ code. Writing "x=x" when x is a smart_ptr may accidentally delete the object to which x points, leading to segfaults.
-
- changed status to open
- removed comment
The patch is simple, but actually getting a segfault to trigger was a bit harder than I expected. #include <iostream> #include "smart_ptr.hpp"
struct A { A() { std::cout << "Construct" << std::endl; } ~A() { std::cout << "Destruct" << std::endl; } // need virtual, otherwise no dereference occurs virtual void foo() { std::cout << "Foo" << std::endl; } };
int main() { cctki_piraha::smart_ptr<A> y(new A(),false); y = y; y->foo(); return 0; }
-
reporter - removed comment
The patch is wrong; you need to skip both the clean() and inc() calls.
I think that "if (this==&s) return *this;" as the first line of operator= should work.
- if this and &s point to the same object, then the assignment operator is a no-op, so nothing needs to be done.
- if the pointers are different, but their guts are the same, then dec/inc should not matter since there are at least two pointers.
-
- removed comment
D'oh. Yes, of course you're right.
-
- removed comment
Patch updated.
-
reporter - changed status to open
- removed comment
Looks right-ish.
-
- changed status to resolved
- removed comment
Fixed in 5082
- Log in to comment