PDA

View Full Version : QObject in stack...??



joseph
26th October 2007, 08:28
Hi guys,

If am cretating a QObject 's object in a stack , will it call the distructor after the "AUTO" scope..

see the below code...


class MyClass : QObject
{
public :

MyClass( const QString & name )
:_name( name ),QObject( )
{

cout<<"\n MyClass() ...";
}
~ MyClass()
{
cout<<__FUNCTION__ ;
}

private:
QString _name;

};

int main( int argc, char *argv[] )
{
QApplication a(argc, argv);

MyClass obj
qDebug()<<"Hello Qt 4.2.2 ";


a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();

}


In this code ( obj in stack ) , ~MyClass() is not getting called.
But if i am creating the "obj " in heap and "delete obj" will call the ~MyClass..

Could you please tell me why this distructor is not getting called while object is created in stack...???

jpn
26th October 2007, 09:07
The output just doesn't get flushed, so add std::endl (or std::flush)

std::cout << "something" << std::endl;
or simply use qDebug()

qDebug() << "something";

wysota
26th October 2007, 09:12
The destructor will be called when the application exits. Try adding std::endl to your cout statement in the destructor.

joseph
26th October 2007, 09:27
The destructor will be called when the application exits. Try adding std::endl to your cout statement in the destructor.

I have used this also , but am not able to see it.
Another thing is , if am debugging using VC Editor ,the controll is not coming to DISTRUCTOR.

Then i tried to LOG the function detail in a file. But there also it is not wrting anything to file.

see the code...


MyClass::~ MyClass()
{
cout<<"\n ~MyClass() ..." <<std::endl;

QFile objFile_2( LOG_FILE );
if ( objFile_2.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream stream( & objFile_2 );
stream << "\n Function Name : " << __FUNCTION__ ;
stream << " [ " << __LINE__ <<" ]" ;
stream << " [ " << __FILE__ <<" ]" ;
}
objFile_2.close();

qDebug()<<"\n ~MyClass() ...";
}


Am i trying something wrong .. please correct me if so ..

pdolbey
26th October 2007, 10:22
If you've lifted the code in the post start from your app you seem to have a missing semi-colon at the end of "MyClass obj"

Have you tried a version with obj on the heap. You'll have finer control over when the destructor is called. Next try a calling a small function with obj on stack; then the destructor will be called when obj goes out of scope. I think trying to do everything in main might cause your problems as you have no control over when objects are destroyed i.e. the application "a" maybe being destroyed before "obj".

Pete

jpn
26th October 2007, 11:52
Have you tried a version with obj on the heap. You'll have finer control over when the destructor is called. Next try a calling a small function with obj on stack; then the destructor will be called when obj goes out of scope.
You can control the life time of a stack object with scopes as well:


void function()
{
Object obj1;

{
Object obj2;

} // obj2 is deallocated

} // obj1 is deallocated




I think trying to do everything in main might cause your problems as you have no control over when objects are destroyed i.e. the application "a" maybe being destroyed before "obj".
Actually, stack objects are deallocated in the exact reverse order they were allocated. Application "a" is allocated first and therefore deallocated last.

pdolbey
28th October 2007, 08:35
Actually, stack objects are deallocated in the exact reverse order they were allocated.

But what order is that? Its not the order that the source code is written, otherwise you'd never be able to write compiler optimisations for loop invariant code.

Pete

marcel
28th October 2007, 08:48
But what order is that? Its not the order that the source code is written, otherwise you'd never be able to write compiler optimisations for loop invariant code.

Pete
It is exactly the order in the code.
What about loop invariant code?

pdolbey
29th October 2007, 17:37
Actually, stack objects are deallocated in the exact reverse order they were allocated. Application "a" is allocated first and therefore deallocated last.

OK, I've spent a jolly hour trying to pursuade the MS compiler to perform a loop invariant construction - I can't do it so... what jpn said seems to be correct (its probably in the standards, but MS wasn't/isn't always compliant with those). I think my memory might have been confused with the issue of global static variables, when the order of construction/destruction is completely indeterminant.

Pete

wysota
29th October 2007, 19:49
I'm a bit lost... what was the original problem?

pdolbey
30th October 2007, 04:30
The OP wasn't see the destructor being called for objects declared in the stack in main().

Pete

wysota
30th October 2007, 09:17
QApplication is declared on stack in main(). Doesn't its destructor get called?