PDA

View Full Version : Deleting Class causes app crash



been_1990
27th January 2010, 23:02
In this thread (http://www.qtcentre.org/threads/27460-QSet-and-custom-class?p=130234), I created my class and made the arrangements for monitoring it.
How can I delete the class? I can't find a way that will work, myne just crashes the app.

void deleteClass(MyClass *class)
{
class->disconnect();
delete class;
}

JohannesMunk
28th January 2010, 12:40
Did you remove your alarm from the map?

QMap<QString, Alarm*> map;
map.remove(name);

Does disconnect work immediately or are there any queued connections (interthread) or posted messages involved?

I need a bigger part of your code to be able to help.

Try to create a minimal working example, that still exhibits the problem!

Johannes

been_1990
29th January 2010, 04:24
Here's the function that creates instances of my class "Alarm":

Alarm *a = new Alarm(file);
alarmList.insert(a->objectName(),a);

ADisplay *display = new ADisplay(this); // other class

connect(a,SIGNAL(soundAlarm(Alarm*)),display,SLOT( showDisplay(Alarm*))); // when its time I call display

connect(display,SIGNAL(quiting()),a,SLOT(quiting() )); // when display is closed call Alarm's quiting() slot (custom) that then:

connect(a,SIGNAL(quiting(Alarm*)),this,SLOT(remAct iveAlarm(Alarm*))); // emit's the quiting() signal passing back to this class the
// object to delete which is:

remActiveAlarm(Alarm *a){
alarmList.remove(a->objectName());
a->disconnect();
delete a;
}

aamer4yu
29th January 2010, 05:43
I can guess 2 things -
One, does remove call destructor too ? Check it, am nto sure.
Second, are you using the Alarm object from where the signal is emitted ? Chances are you are using. So when the code reaches back after the remActiveAlarm() slot, you have a invalid object and hence you get crash.
As a remedy, trying a->deleteLater().

JohannesMunk
29th January 2010, 10:55
Obviously we still have to guess, how the rest of the code may look like.. We need more. Try to build a small compilable example, that still crashes.

Johannes

been_1990
29th January 2010, 12:34
Here attached is a small working(crashing) example.

JohannesMunk
29th January 2010, 12:54
Hi there!



~Alarm()
{
delete this;
}

That is calling the destructor recursively, until a stack overflow happens. The destructor is called, when you delete an object. No reason to delete it again..



~Alarm()
{
}

You only delete child objects in the destructor..

Joh

JohannesMunk
29th January 2010, 13:19
One, does remove call destructor too ? Check it, am nto sure.

It does not, because he is using pointers in his collection. Pointers are not automatically deleted.

If you want to properly clear a collection (map, hash, list, ..) of pointers you need to:



qDeleteAll(list);
list.clear();


Or one by one, with delete and remove.. as been_1990 is doing..

Johannes

been_1990
29th January 2010, 18:52
Thanks JohannesMonk. That was the problem.