Results 1 to 10 of 10

Thread: Destructor not running

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 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?

  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

    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 12:25.

  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

    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, 19:54
  2. Destructor overriding
    By Daylight in forum Qt Programming
    Replies: 13
    Last Post: 1st March 2013, 11:05
  3. Destructor not called
    By satoshi in forum General Programming
    Replies: 2
    Last Post: 23rd April 2010, 13:55
  4. Destructor in QT
    By hgedek in forum Newbie
    Replies: 1
    Last Post: 18th September 2007, 10:54
  5. Replies: 1
    Last Post: 17th May 2006, 00: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
  •  
Qt is a trademark of The Qt Company.