Results 1 to 10 of 10

Thread: Destructor not running

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Destructor not running

    I have created a class for logging of errors into a text file, and the logging works, however I'm not seeing the destructor ever run.
    I am testing this by having the destructor write to the file.
    Can someone explain when the destructor is actually supposed to be called?

    Thanks, this is my full code. If you can suggest a better way to code, please let me know, as I don't like doing things that work, but are not best coding practices.

    Qt Code:
    1. #include "errorlogging.h"
    2.  
    3. ErrorLogging::ErrorLogging()
    4. {
    5. QString fileName = "c:/ProductionDB/SIMSErrors.daq";
    6. m_File = new QFile(fileName);
    7. m_File->open(QIODevice::Append | QIODevice::Text);
    8. }
    9.  
    10. void ErrorLogging::logError(QString timeStamp, QString classInstance, QString errorMsg)
    11. {
    12. QTextStream logStream(m_File);
    13. logStream << timeStamp << classInstance << errorMsg << "\n";
    14. }
    15.  
    16. ErrorLogging::~ErrorLogging()
    17. {
    18. QTextStream logStream(m_File);
    19. logStream << "File closing...";
    20. m_File->close();
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not running

    This looks ok, aside from m_File never being deleted, but is definitely not your full code.
    This lacks the place where the class is actually instantiated.

    In general:
    - if you create the object of a class on the stack, it will be deleted when the current scope ends, e.g. the function scope.
    - if you create the object of a class on the heap, then you need to explicitly delete it

    For the latter case there are also classes called smart pointers which can bind heap allocated objects to scopes or getting refernce counted handles, etc.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Destructor not running

    Hi anda_skoa, you seem to be the one who most often gives advice, thank you.

    In another class I have the following where I instantiate this error logging class, along with a test of the logError method.
    After this, I only close the application, believing[/hoping!] that all destructors then get called automatically.

    Qt Code:
    1. m_ErrorLog = new ErrorLogging;
    2. m_ErrorLog->logError("1", "2", "3");
    To copy to clipboard, switch view to plain text mode 

    Also, would this be the new destructor then, in order to delete the m_File variable?

    Qt Code:
    1. ErrorLogging::~ErrorLogging()
    2. {
    3. QTextStream logStream(m_File);
    4. logStream << "File closing...\n";
    5. m_File->close();
    6. delete m_File;
    7. }
    To copy to clipboard, switch view to plain text mode 

    I declare m_File as

    Qt Code:
    1. private:
    2. QFile *m_File;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not running

    Quote Originally Posted by ShamusVW View Post
    After this, I only close the application, believing[/hoping!] that all destructors then get called automatically.
    Very bad practice. By the way I think you should read about the singleton.

  5. #5
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Destructor not running

    I know about the Singleton, but not sure how it applies to this?
    Basically to create a single instance of an object, and if further objects get created, they revert to the original one created.
    Don't ask me to program it though, because in that case I WILL have to go and read up on it, it has been a long while.

    My understanding with destructors is that they got called automatically when the object went out of scope as andy_skoa mentioned, however, when the application terminates, why is the error logger class's destructor not called?
    Surely the object gets destroyed when the application ends, and hence the destructor should be called?

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not running

    According to me this is a typical situation when you use a singleton - the entire application should use one instance of logger.

    Object constructed by new is never going "out of scope". Objects are terms of language not an application. You can mix languages in one application. The application manages the memory, not objects.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not running

    Quote Originally Posted by ShamusVW View Post
    After this, I only close the application, believing[/hoping!] that all destructors then get called automatically.
    When the process ends, the operating system will reclaim things like memory, open files, etc.,
    But once the program has ended it can't execute any code anymore (well, otherwise it would not have ended, would it )
    So only destructors of objects get executed that get destroyed while the program is still executing.

    Quote Originally Posted by ShamusVW View Post
    Qt Code:
    1. m_ErrorLog = new ErorLogging;
    2. m_ErrorLog->logError("1", "2", "3");
    To copy to clipboard, switch view to plain text mode 
    This creates an object on the heap. If there is no matching delete, then the object remains indefinitely.

    Quote Originally Posted by ShamusVW View Post
    Also, would this be the new destructor then, in order to delete the m_File variable?
    Yes.
    You could even drop the call to close(), the QFile destructor will do this anyway.

    Cheers,
    _

  8. #8
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Destructor not running

    Ok, thanks, I understand what you are meaning to use it for.
    The way I was planning to do it is pass the object along to each class/object as needed.
    So if I created a new object from the class where I first created the logger object (i.e. m_ErrorLog), I would then use: anotherObject = new className anotherObject(m_ErrorLog); <--- passing the object between other objects created
    Then in the newObject, I will then just reference the logging method (logError), I wouldn't instantiate a new logger object again.

    What I have read is that there is debate on the use of the Singleton class, and that there are "better" ways to do things.
    I don't have enough experience and knowledge to have an opinion, but it seemed to me that if people were throwing into question using it, then I should look at other ways to do it.
    Is my passing of objects around bad?

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not running

    Quote Originally Posted by ShamusVW View Post
    My understanding with destructors is that they got called automatically when the object went out of scope as andy_skoa mentioned
    Yep, they do.

    Quote Originally Posted by ShamusVW View Post
    however, when the application terminates, why is the error logger class's destructor not called?
    Because the object that goes out of scope in your case is the pointer to your error logger, not the error logger itself.
    The pointer is a primitive datatype (like an int), it doesn't have any destructor (it isn't a class, just an address value).

    Quote Originally Posted by ShamusVW View Post
    Surely the object gets destroyed when the application ends, and hence the destructor should be called?
    No. Your application code never destroys the object, i.e. you never call delete on the pointer to the object.


    Added after 10 minutes:


    Quote Originally Posted by ShamusVW View Post
    The way I was planning to do it is pass the object along to each class/object as needed.
    So if I created a new object from the class where I first created the logger object (i.e. m_ErrorLog), I would then use: anotherObject = new className anotherObject(m_ErrorLog); <--- passing the object between other objects created
    Then in the newObject, I will then just reference the logging method (logError), I wouldn't instantiate a new logger object again.
    Sensible approach.

    Quote Originally Posted by ShamusVW View Post
    What I have read is that there is debate on the use of the Singleton class, and that there are "better" ways to do things.
    I don't have enough experience and knowledge to have an opinion, but it seemed to me that if people were throwing into question using it, then I should look at other ways to do it.
    Is my passing of objects around bad?
    A singleton has a couple of properties/behaviors and it is sometimes used for only one of them and not always is that a good reason.

    One thing a singleton is good at is the use case where there absolutely can only be one instance.
    A singleton class' constructor is usually private, only the instance() method can create an object of the class.

    Sometimes,however, singletons are just used like global variables, so the usual arguments against global variables apply.

    Loggers often use it for a combination, i.e. both for the convenience of global access as well as having a central object that all logging passes through.
    But I've also seen loggers being passed through, e.g. when applications have multuple loggers or components are allowed to create their own, local, loggers.

    One thing with singletons is that their objects are often never explicitly destroyed or such explicit destruction needs to be added to the end of main()
    (while instantiation is often implicit in the first call to the instance access function)

    Cheers,
    _
    Last edited by anda_skoa; 22nd August 2016 at 13:25.

  10. #10
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Destructor not running

    Thank you Lesiok & Anda_skoa for your feedback on destructors, object passing and the Singleton pattern.
    It has given me direction to investigate this further, as I need to brush up on destructors and when they run, and also deleting of objects.

Similar Threads

  1. Replies: 12
    Last Post: 18th September 2014, 20:54
  2. Destructor overriding
    By Daylight in forum Qt Programming
    Replies: 13
    Last Post: 1st March 2013, 12:05
  3. Destructor not called
    By satoshi in forum General Programming
    Replies: 2
    Last Post: 23rd April 2010, 14:55
  4. Destructor in QT
    By hgedek in forum Newbie
    Replies: 1
    Last Post: 18th September 2007, 11:54
  5. Replies: 1
    Last Post: 17th May 2006, 01:23

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.