C ++ - when are class members without pointers destroyed?

Suppose I have this code ...

class GraphFactory : public QObject
{
private:
    QMap<QString, IGraphCreator*> factory_;

public:
    virtual ~GraphFactory();
};

GraphFactory::~GraphFactory()
{
    // Free up the graph creators
    QMap<QString, IGraphCreator*>::iterator itr;
    for (itr = factory_.begin(); itr != factory_.end(); itr++)
    {
        IGraphCreator * creator = itr.value();
        delete creator;
        creator = NULL;
    }

}

When is the QMap factory destroyed? Before calling the destructor or during the destructor? (I understand that the destructor will be called when the GraphFactory instance goes out of scope. But when does the element without pointers get destroyed?)

Edit: I get invalid values ​​for the factory_ map when it reaches the destructor. A breakpoint indicates that the value would not affect the value stored inside the QMap.

+3
source share
3 answers

It will be destroyed after the destructor code is executed.

, , .

+11

GraphFactory::~GraphFactory, QObject::~QObject. .

+2

, GraphFactory:: factory_, , , , . , GraphFactory, , , - .

Windows, , Application Verifier, ( " " ).

If you use your GraphFactory class in a multi-threaded environment, you should synchronize access to the QMap instance, because the “bad things” can happen if you do not.

I do not know equivalent tools for other platforms. I would love to hear about them if anyone reads this to find out.

+1
source

Source: https://habr.com/ru/post/1719369/


All Articles