PDA

View Full Version : Problem with Destructor of QObject



mikro
9th July 2006, 14:43
i get the Message
"neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined." for a class that is derived from QObject where i am trying to have a destructor.
and i just can't figure out what it means and what i could do against it. Could somebody please enlighten me?

jacek
9th July 2006, 16:04
Could you post the exact error message and the part of your code it refers to? How did you define your class?

mikro
9th July 2006, 16:44
that is allready the complete errormessage. it refered to the point in the destructor were i was using delete on a pointer that i store as a private attribute to my class. Now that i have commented out that line i don't get this error anymore.
It seems the problem is, that the pointer points to an instance of a class which i had to forward declarate. Not being experienced in C++ i am fighting with a DoD-Problem here:

i have a class which depicts an asset (for example a pump or a pipe or else). The assets can have attributes like length, width and things like that. But the attributes can also be links to a different asset (as a pipe would have to know of the two manholes between it is to get the depth) or they could be keys for a details-Table. Therefore there are 4 Attribute-Type-Classes which all inherit a masterclass attribute. Then i have a third class with costs. For a pipe one of the costs would be the materialprice of the pipe itself. So this instance of the costclass should know that the price can be calculated with lengthOfPipe * PriceFor1m and it should of course also know where to find the price for 1m

Now the problem:
I want to be able to ask the assetclass what this asset costs. So the asset will go through all the cost-classes attached to her (actually the asset class stores a QMap<QString,cCost> and ask each of those costs what they cost and sum it up. To do that those costs will need to get information about the asset: in the example above the length of the pipe is stored with the asset.
So both classes need to use each others methods, i either get a DoD or Problems like the above ;(

jacek
9th July 2006, 16:56
the pointer points to an instance of a class which i had to forward declarate.
If you have only forward declaration, then you can't invoke any method, constructor or destructor of that class. Compiler simply doesn't know if they exist. You must add proper #include directive.


So both classes need to use each others methods, i either get a DoD or Problems like the above ;(
You can solve it with forward declarations, like this:
// a.h
class B; // forward declaration only, so you can't use B instances here

class A
{
...
void someMethod( const B& b ); // this will work
void someOtherMethod( B* b ); // this is also ok
...
private:
B *_b; // note that you can use only a pointer here, since you can't include class definition
};

//-------------------------------------------------------------------
// b.h

class A;

class B
{
...
private:
A *_a;
};

//-------------------------------------------------------------------
// a.cpp

#include "a.h"
#include "b.h"

vouid A::someMethod( const B& b )
{
// you can use B here
}

mikro
9th July 2006, 19:41
part of me wanted to say "yes, i know all that" but actually you have given me the answer: my problem was that i didn't realize, that just because i cannot have both headerfiles include the other doesn't mean that i cannot include them both in the .cpp and there use all the methods of the other class i need.
nevertheless i still don't understand why there should be a problem with deleting that pointer and neither what that errormessage was suppossed to tell me.
all i had was:

anlage.h:

#include "kosten.h"
class cAttribut;
class cMassnahme;

class cAnlage : public QTreeWidgetItem {

public:
~cAnlage() {
if (this->isDirty) {
writeDB();
}
delete anlagenart;
// delete mMassnahme;
// qDeleteAll(Attribute);
qDeleteAll(kosten);
};
protected:
QMap<QString,cAttribut*> Attribute;
QMap<QString,cKosten*> kosten;
private:
bool isDirty;
QSqlDatabase db;
cMassnahme* mMassnahme;

cAttribut and cMassnahme are only forward-declarated as i need to fully include this headerfile in their headers, whereas cKosten is in the included file kosten.h. As you can see i had to comment out the lines deleting the pointers to those classes that had only been forward declarated whereas i can still delete kosten.

I have now put the implementation of the destructor over to the .cpp where i can include the header and it works ;)
is it normal that you can't delete a pointer of a type that is only forward-declarated?

jacek
9th July 2006, 20:05
is it normal that you can't delete a pointer of a type that is only forward-declarated?
Yes, forward declaration only tells the compiler that such class exists, but the compiler has to know where exactly the destructor is located to be able to use it --- forward declaration doesn't provide this information.