Re: QObject in stack...??
The output just doesn't get flushed, so add std::endl (or std::flush)
Code:
std::cout << "something" << std::endl;
or simply use qDebug()
Code:
qDebug() << "something";
Re: QObject in stack...??
The destructor will be called when the application exits. Try adding std::endl to your cout statement in the destructor.
Re: QObject in stack...??
Quote:
Originally Posted by
wysota
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...
Code:
MyClass::~ MyClass()
{
cout<<"\n ~MyClass() ..." <<std::endl;
QFile objFile_2
( LOG_FILE
);
{
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 ..
Re: QObject in stack...??
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
Re: QObject in stack...??
Quote:
Originally Posted by
pdolbey
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:
Code:
void function()
{
Object obj1;
{
Object obj2;
} // obj2 is deallocated
} // obj1 is deallocated
Quote:
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.
Re: QObject in stack...??
Quote:
Originally Posted by
jpn
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
Re: QObject in stack...??
Quote:
Originally Posted by
pdolbey
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?
Re: QObject in stack...??
Quote:
Originally Posted by
jpn
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
Re: QObject in stack...??
I'm a bit lost... what was the original problem?
Re: QObject in stack...??
The OP wasn't see the destructor being called for objects declared in the stack in main().
Pete
Re: QObject in stack...??
QApplication is declared on stack in main(). Doesn't its destructor get called?