Skip to content
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

Setting member pointers to nullptr in destructor may not be good practice #1154

Open
fcollot opened this issue Oct 13, 2023 · 0 comments
Open

Comments

@fcollot
Copy link
Contributor

fcollot commented Oct 13, 2023

Following my remarks on #1131 I made a small test.

First I created the following class:

class Foo
{
public:
    int* i;

    Foo()
    {
        i = new int;
    }

    ~Foo()
    {
        delete i;
        i = nullptr;
    }
};

and executed this code:

Foo* foo = new Foo();
qDebug() << foo->i;
delete foo;
qDebug() << foo->i;

In debug build the pointer is being set to nullptr, but in release build it is not.

EDIT: I removed the test on double delete as it is not relevant. The first test proves that setting to nullptr will not work in release, and so will not protect from double delete as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant