-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check _ptr != ptr is not enough in AutoPtr.assign #3979
Comments
@funny-falcon how does the dangling pointer happen exactly? the check is whether the assigned-from |
Using poco 1.11.0 from Ubuntu 23.04 #include <iostream>
#include <string>
#include <Poco/RefCountedObject.h>
#include <Poco/AutoPtr.h>
class RCO : public Poco::RefCountedObject
{
public:
RCO(int _i) : i(_i), name("RCO(" + std::to_string(_i) + ")")
{
std::cout << name << " constructed\n";
}
int i;
std::string name;
Poco::AutoPtr<RCO> linked;
~RCO()
{
std::cout << name << " destructed\n";
name = "BOOMED " + name;
}
};
Poco::AutoPtr<RCO>
make_two()
{
Poco::AutoPtr<RCO> a(new RCO(1));
a->linked = new RCO(2);
return a;
}
int
main() {
Poco::AutoPtr<RCO> a;
a = make_two();
std::cout << "will it boom?\n";
a = a->linked;
std::cout << "hello " << a->name << "\n";
} Output:
|
Is it desired behavior? |
I'd say, it is not a desirable use - you are assigning a member You can do this instead and everything will be fine:
|
Common knowledge in reference counting is "on assignment increment first then decrement", because "just to be deleted" object could hold last reference to "just to be assigned" one. Fixes pocoproject#3979
Common knowledge in reference counting is "on assignment increment first then decrement", because "just to be deleted" object could hold last reference to "just to be assigned" one. Fixes #3979
Common knowledge in reference counting is "on assignment increment first then decrement", because "just to be deleted" object could hold last reference to "just to be assigned" one. Fixes #3979
poco/Foundation/include/Poco/AutoPtr.h
Lines 122 to 127 in 3838070
New object could be one referenced by old with it's internal AutoPtr.
If we
release
old pointer beforeduplicate
new we could have dangled pointer problem already.The text was updated successfully, but these errors were encountered: