Classes that can crash during initialization

This is a discussion on Classes that can crash during initialization within the C++ Programming forums, part of the General Programming Boards category; >> Although RAII does stand for resource aquisition is intialisation. You are correct. http://www.parashift.com/c++-faq-lit....html#faq-6.18 has some info on the confusion ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> Although RAII does stand for resource aquisition is intialisation.
    You are correct. http://www.parashift.com/c++-faq-lit....html#faq-6.18 has some info on the confusion between the name and what it refers to (that FAQ mentions "Resource Reclamation Is Destruction" might be a better name).

    Also, the entire FAQ #17 at that site has good information on the subject of RAII and exception handling.

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Simple solution is never do anything but variables initialization in constructors. Don't perform dynamic memory allocation, opening files, etc, etc. Anything that has a chance of failing should not be performed inside of a constructor. Only perform operations that will succeed 100% of the time. Initializing variables of your class to constant values will always succeed.

    If you absolutely must allocate a resource then use the provided try and catch mechanism and even perhaps write your own exception class and create an object of that type to handle the exception. Some exceptions are non-recoverable no matter what you do, which is why I advise not to perform any operation inside of a constructor that can throw an exception or fail.
    Last edited by VirtualAce; 06-26-2006 at 11:16 PM.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> Simple solution is never do anything but variables initialization in constructors. Don't perform dynamic memory allocation, opening files, etc, etc.
    I completely disagree. Use the constructor to construct and fully initialize the object so that it is ready to use. That means that if your class requires dynamic memory allocation, opening a file, etc, you should absolutely do that in the constructor. Just take the precautions already discussed in this thread and in the linked FAQ (e.g. RAII).

    >> Some exceptions are non-recoverable no matter what you do, which is why I advise not to perform any operation inside of a constructor that can throw an exception or fail.
    This is exactly why you should perform operations that can throw or fail inside the constructor. Otherwise, you are left with an object that is uninitialized and cannot do its job. If you let it fail inside the constructor, then the user of your class doesn't have to worry about checking to see if the object has been initialized correctly or not. If the object exists, it means the constructor succeeded, which means the instance is initialized.

    It might make sense to postpone initialization if you are using a lazy evaluation optimization, or if your code doesn't use exceptions and would be negatively affected by adding them. Otherwise, do all your initialization in the constructor.
    Last edited by Daved; 06-27-2006 at 11:17 AM.

  4. #19
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I completely disagree. Use the constructor to construct and fully initialize the object so that it is ready to use. That means that if your class requires dynamic memory allocation, opening a file, etc, you should absolutely do that in the constructor. Just take the precautions already discussed in this thread and in the linked FAQ (e.g. RAII).
    We obviously won't agree here. I do not recommend this.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    I have never heard of somebody arguing this position. I assume you would use a separate init or open function to do the resource acquisition. If not, please clarify. Do you have any arguments for why that would be better than RAII and exceptions?

  6. #21
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Thanks for all the help guys, it's much appreciated.

    I read the bits on parashift about RAII and I'm mildly confused about the 'renaming' of the acronym. I understand what it's saying, but I thought one of the big things about RAII was that if you initialize an object on creation then you save the overhead of using the default constructor and then the copy one (which I heard was significant).

    To implement RAII in my example does that just mean initialising the member objects in an initialization list in the constructor?

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, in your example you would have to replace the A* with a smart pointer. Initializing it in the initialization list is a good idea, but not necessary.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Don't worry about what RAII stands for, it's not really important. It just means that when you create an object it acquires its resources, and then it automatically cleans itself up when it is destructed.

    That is why a smart pointer that holds A* would help. It is the smart pointer that uses RAII, not necessarily your class. If you replace A* with a smart pointer that calls delete automatically when it goes out of scope, then it doesn't matter if your object is destroyed normally, or if it gets destructed due to an exception from b.init(). Either way the smart pointer's destructor will be called, and delete will be called on your A*.

    >> I thought one of the big things about RAII was that if you initialize an object on creation then you save the overhead of using the default constructor and then the copy one (which I heard was significant).

    This is an important concept, although I don't think this is a specific benefit of RAII. Technically, a user of a class that models RAII could still violate that rule. For example, std::string uses RAII, but you can still use string's default constructor and then assign to the string later.
    Last edited by Daved; 06-27-2006 at 04:08 PM.

  9. #24
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Daved
    I have never heard of somebody arguing this position. I assume you would use a separate init or open function to do the resource acquisition. If not, please clarify. Do you have any arguments for why that would be better than RAII and exceptions?
    I agree with Daved on this one. The problem with seperate init functions is that you can forget to call them or if you use your class with a template or framework that doesn't call them you're stuffed.
    (I ran into this problem recently when using Boost.Statechart which uses constructors and destructors and entry and exit conditions of states)

    The only time you should use an "init" function is when you want to have polymorphic initialisation behaviour, in which case you should make your constructor private and use a static Create function to create your class

    http://www.parashift.com/c++-faq-lit....html#faq-23.5
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Derived classes and initialization lists
    By DominicTrix in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2004, 05:15 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 05:49 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21