Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Article: The RAII Programming Idiom - Comments Please

Options
  • 23-07-2003 6:40pm
    #1
    Closed Accounts Posts: 9,314 ✭✭✭


    The RAII Programming Idiom
    I’m often surprised, and occasionally horrified, that Resource Acquisition Is Initialisation isn’t better known than it is amongst programmers who use languages to which it is applicable. It is a simple, eloquent and efficient way to deal with many situations where there is a risk of “leaking” memory or handles, failing to release file locks, etc.
    Comments Please.


Comments

  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Honestly, I think it might be a overkill to have a specific class to do resource allocation and cleanup.

    Then again, it really depends on how much dynamic data you have, at a certain point, I don't see how one could reasonably avoid having a 'safe' way to allocate and cleanup resources using an inherited class.

    So, I guess it really depends on the scope of your application and what your priorites are.

    Speed of execution and minimalist use of memory and CPU execution time or an attempt to become monotheistic about memory usage and cleanup.

    If you trust your-self and your development team to be neat, then RAII is superflous and if similarly if you have coders you think, 'sometimes' need a great big nudge in the direction of enforced garbage collection, then maybe this is the way.... and then again, maybe not.


  • Registered Users Posts: 4,676 ✭✭✭Gavin


    I wasn't aware of the pattern itself, but have used it on occasion and one problem I encountered was error checking.

    If you create an instance of your wrapper class which is responsible for file access say, or perhaps allocating memory internally, how do you effectively report a problem such as no file or lack of memory.
    I believe you can't throw exceptions from a constructor ?
    So you may have to set a marker and check for this, or throw exceptions from the class's methods..

    And of course, you have to remember to dispose correctly of your wrapper class to !

    my random babbles.

    Gav


  • Registered Users Posts: 1,931 ✭✭✭Zab


    I think RAII really comes into its own when combined with exceptions, especially when they can come from nested methods. I wouldn't use it for everything myself, just where it fits (ie complexity means that easy of use becomes more important than the very slight inefficiency involved).

    Verb: You can throw an exception from a constructor. And if you don't like that you can have a method that initializes the resource after construction and returns a value. And you don't really have to dispose of the wrapper object, the point is that you are using stack unwinding and automatic (stack based) variables to do just that. All you have to do is make sure it goes out of scope at the correct time.

    Zab


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Cool, I'm going to go back to the article and address everything mentioned here.
    Originally posted by Verb
    I believe you can't throw exceptions from a constructor ?

    You can throw exceptions in constructors and destructors. Indeed this is one of the advantages of exceptions over returning a value indicating success (other advantages being efficiency, good separation between "normal" and exception-handling code, and better defined exception semantics).

    The way exceptions interact with constructors and destructors is quite important to RAII, so I'll mention it in my rewrite.


  • Registered Users Posts: 4,676 ✭✭✭Gavin


    Ah right. Cheers, dunno where I picked up that useless bit of information. Must be more discerning in the future when reading stuff off the internet.

    Gav


  • Advertisement
  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Right, re-write up at same address as before.


  • Registered Users Posts: 491 ✭✭Silent Bob


    I saw the subject and didn't have a clue what RAII was, then I saw the de-acronymised version and still didn't really know what you were talking about. Then I read the article and realised I've been doing it for ages anyway :)

    One thing I dislike about Java (the language being used for my current project) is the lack of a destructor. Having to explicitly call a cleanup method for classes that need it feels dirty and not proper OO.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by Silent Bob
    I saw the subject and didn't have a clue what RAII was, then I saw the de-acronymised version and still didn't really know what you were talking about. Then I read the article and realised I've been doing it for ages anyway :)
    That's often true about idioms and patterns in general, as I say in the piece there's a big advantage in identifying patterns you already use, because you are then using them consciously.


Advertisement