Reputation: 767
I have a logging interface that allows user to subclass a Logger
and override the log()
virtual function so users can create their own loggers. There is a manager that keeps track of all the registered logging methods (i.e. Console, syslog, Widget, etc.)
I've created a QListWidget Logger that shows logs. Unfortunately it's owned by the logging manager and since it's a QObject, it's also a child of the MainWindow. This results in two objects trying to delete it upon aplication close.
The first time around the QObjects->deleteChildren()
gets called and it deletes it. Then the Loggin gmanager tries again and CRASH.
How can I prevent a specific QObject from being destroyed during deleteChildren()
? Is that a bad idea?
Upvotes: 3
Views: 2433
Reputation: 25165
You can't exclude a child from deletion, and if you could, that'd be a bad idea. In the QObject hierarchy, the child is owned by the parent. That's one of the main purposes of the parent-child relationship. I don't see why you should want a parent-child relationship without the deletion.
Possibilities:
Without knowing the code, I'd say the mainwindow shouldn't mess with the logger, so I would move the ownership completely to the manager.
Also, I don't see a reason why the usual Logger interface should inherit from QObject at all, so I'd consider removing that inheritance completely (which leaves the "owned solely by Logging manager" and QSharedPointer options).
Upvotes: 7