Results 1 to 12 of 12

Thread: QObject in stack...??

  1. #1
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default QObject in stack...??

    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...
    Qt Code:
    1. class MyClass : QObject
    2. {
    3. public :
    4.  
    5. MyClass( const QString & name )
    6. :_name( name ),QObject( )
    7. {
    8.  
    9. cout<<"\n MyClass() ...";
    10. }
    11. ~ MyClass()
    12. {
    13. cout<<__FUNCTION__ ;
    14. }
    15.  
    16. private:
    17. QString _name;
    18.  
    19. };
    20.  
    21. int main( int argc, char *argv[] )
    22. {
    23. QApplication a(argc, argv);
    24.  
    25. MyClass obj
    26. qDebug()<<"Hello Qt 4.2.2 ";
    27.  
    28.  
    29. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    30. return a.exec();
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    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...???

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QObject in stack...??

    The output just doesn't get flushed, so add std::endl (or std::flush)
    Qt Code:
    1. std::cout << "something" << std::endl;
    To copy to clipboard, switch view to plain text mode 
    or simply use qDebug()
    Qt Code:
    1. qDebug() << "something";
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject in stack...??

    The destructor will be called when the application exits. Try adding std::endl to your cout statement in the destructor.

  4. #4
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QObject in stack...??

    Quote Originally Posted by wysota View Post
    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...
    Qt Code:
    1. MyClass::~ MyClass()
    2. {
    3. cout<<"\n ~MyClass() ..." <<std::endl;
    4.  
    5. QFile objFile_2( LOG_FILE );
    6. if ( objFile_2.open( QIODevice::WriteOnly | QIODevice::Append ) )
    7. {
    8. QTextStream stream( & objFile_2 );
    9. stream << "\n Function Name : " << __FUNCTION__ ;
    10. stream << " [ " << __LINE__ <<" ]" ;
    11. stream << " [ " << __FILE__ <<" ]" ;
    12. }
    13. objFile_2.close();
    14.  
    15. qDebug()<<"\n ~MyClass() ...";
    16. }
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QObject in stack...??

    Quote Originally Posted by pdolbey View Post
    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:
    Qt Code:
    1. void function()
    2. {
    3. Object obj1;
    4.  
    5. {
    6. Object obj2;
    7.  
    8. } // obj2 is deallocated
    9.  
    10. } // obj1 is deallocated
    To copy to clipboard, switch view to plain text mode 

    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.
    J-P Nurmi

  7. #7
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject in stack...??

    Quote Originally Posted by jpn View Post
    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

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject in stack...??

    Quote Originally Posted by pdolbey View Post
    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?

  9. #9
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject in stack...??

    Quote Originally Posted by jpn View Post
    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject in stack...??

    I'm a bit lost... what was the original problem?

  11. #11
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject in stack...??

    The OP wasn't see the destructor being called for objects declared in the stack in main().

    Pete

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject in stack...??

    QApplication is declared on stack in main(). Doesn't its destructor get called?

Similar Threads

  1. Replies: 1
    Last Post: 7th August 2007, 08:27
  2. Reparenting a QObject
    By ghorwin in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2007, 17:21
  3. Interface composition and QObject
    By brcain in forum Qt Programming
    Replies: 9
    Last Post: 20th November 2006, 17:56
  4. Previewing a stack widget dialog
    By mikeh in forum Qt Tools
    Replies: 4
    Last Post: 2nd October 2006, 10:19
  5. QWidget display on 2 stack widget page
    By spawnwj in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2006, 12:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.