Thread: Why would the destructor be called?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    16

    Why would the destructor be called?

    In the following code:

    Code:
    iniFileParser parser = iniFileParser(fname);
    if(!parser.getValue("State", "width", &width))
    {
    ...
    }
    It seems that the constructor is being called between the time I create the parser and the time that it is used one line below. Any ideas why this might be the case?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Assuming that you are talking about an iniFileParser constructor, it would be because this constructs an iniFileParser object named parser:
    Code:
    iniFileParser parser = iniFileParser(fname);
    If you are talking about the iniFileParser destructor, then it would be because you create a temporary and copy from it (after which the copy is destroyed), and the compiler did not optimise away the extra copy in this case, perhaps because optimisations were not enabled. You should just write:
    Code:
    iniFileParser parser(fname);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Thanks...that makes sense!

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    If you are doing something with side effects in the destructor (eg, print a message), the compiler can't optimize it out, otherwise it will be changing the behaviour of your program.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cyberfish View Post
    If you are doing something with side effects in the destructor (eg, print a message), the compiler can't optimize it out, otherwise it will be changing the behaviour of your program.
    Yes it can. The C++ standard explicitly allows the compiler to eliminate any object if the only way of detecting the existence of that object is by tracking calls of constructors and destructors. This specifically allows the compiler to avoid constructing or destructing objects unnecessarily. A side effect of that is that any side-effects of constructors and destructors are not guaranteed to occur.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah never knew that.

    Stand corrected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2009, 12:05 AM
  2. Replies: 4
    Last Post: 09-21-2008, 02:27 PM
  3. callback from exe called with system()
    By leonv in forum C Programming
    Replies: 3
    Last Post: 01-25-2008, 04:12 PM
  4. AAARG!!! mental block, what is this character called: ')'
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-29-2002, 10:29 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM