QSA's children objects deletion problem

78 views Asked by At

I've some problems with deleting QSA's referred objects. In constructor, I've wrote:

  QSProject * project = {initialization of QSProject}
  MyWrapper * wrapper = new MyWrapper; // MyWrapper is QObject's child. It comes without parent here
  project->addObject(wrapper);

I've wrote in descructor:

  project->clearObjects();
  delete project;
  delete wrapper;

This code cause to segfault at destructor's execution, exactly -- when I'm trying to delete wrapper.

I've made some research and I know that:

  1. QSProject doesn't delete his "children objects", so this is not a "double delete" problem
  2. If I don't add wrapper to QSProject in constructor, it works well.
  3. If I don't delete wrapper in descructor, it works well (but memory leaks).

What's up?

1

There are 1 answers

1
Casey On

First, I'm not familiar with QSA, but given how the Qt API usually works QSProject is likely taking ownership of the object. This means QSProject is reparenting the object using QObject::setParent().

In which case you are doubly deleting the object.

Have you verified with a tool such as valgrind that you are in fact leaking memory when you omit the delete?