#1521: Piraha smart_ptr can't handle self assignment ----------------------+----------------------------------------------------- Reporter: eschnett | Owner: Type: defect | Status: new Priority: major | Milestone: Component: Cactus | Version: development version Keywords: | ----------------------+----------------------------------------------------- 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.
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: assigned Priority: major | Milestone: Component: Cactus | Version: development version Resolution: | Keywords: -----------------------+---------------------------------------------------- Changes (by knarf):
* owner: => sbrandt * status: new => assigned
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: assigned Priority: major | Milestone: Component: Cactus | Version: development version Resolution: | Keywords: -----------------------+----------------------------------------------------
Comment (by knarf):
Just curious: why would you need this in a parameter file?
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: assigned Priority: major | Milestone: Component: Cactus | Version: development version Resolution: | Keywords: -----------------------+----------------------------------------------------
Comment (by eschnett):
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.
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: review Priority: major | Milestone: Component: Cactus | Version: development version Resolution: | Keywords: -----------------------+---------------------------------------------------- Changes (by sbrandt):
* status: assigned => review
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; }
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: review Priority: major | Milestone: Component: Cactus | Version: development version Resolution: | Keywords: -----------------------+----------------------------------------------------
Comment (by eschnett):
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.
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: review Priority: major | Milestone: Component: Cactus | Version: development version Resolution: | Keywords: -----------------------+----------------------------------------------------
Comment (by sbrandt):
D'oh. Yes, of course you're right.
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: review Priority: major | Milestone: Component: Cactus | Version: development version Resolution: | Keywords: -----------------------+----------------------------------------------------
Comment (by sbrandt):
Patch updated.
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: reviewed_ok Priority: major | Milestone: Component: Cactus | Version: development version Resolution: | Keywords: -----------------------+---------------------------------------------------- Changes (by eschnett):
* status: review => reviewed_ok
Comment:
Looks right-ish.
#1521: Piraha smart_ptr can't handle self assignment -----------------------+---------------------------------------------------- Reporter: eschnett | Owner: sbrandt Type: defect | Status: closed Priority: major | Milestone: Component: Cactus | Version: development version Resolution: fixed | Keywords: -----------------------+---------------------------------------------------- Changes (by sbrandt):
* status: reviewed_ok => closed * resolution: => fixed
Comment:
Fixed in 5082
trac@lists.einsteintoolkit.org