Deleting Class causes app crash
In this thread, 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.
Code:
void deleteClass(MyClass *class)
{
class->disconnect();
delete class;
}
Re: Deleting Class causes app crash
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
Re: Deleting Class causes app crash
Here's the function that creates instances of my class "Alarm":
Code:
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(remActiveAlarm(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;
}
Re: Deleting Class causes app crash
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().
Re: Deleting Class causes app crash
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
1 Attachment(s)
Re: Deleting Class causes app crash
Here attached is a small working(crashing) example.
Re: Deleting Class causes app crash
Hi there!
Code:
~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..
You only delete child objects in the destructor..
Joh
Re: Deleting Class causes app crash
Quote:
Originally Posted by
aamer4yu
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:
Code:
qDeleteAll(list);
list.clear();
Or one by one, with delete and remove.. as been_1990 is doing..
Johannes
Re: Deleting Class causes app crash
Thanks JohannesMonk. That was the problem.