RAII (Resource Acquisition Is Initialization) pattern limitations

This is a discussion on RAII (Resource Acquisition Is Initialization) pattern limitations within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Through my testing and study of RAII (Resource Acquisition Is Initialization) pattern, I think it can solve resource ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    RAII (Resource Acquisition Is Initialization) pattern limitations

    Hello everyone,


    Through my testing and study of RAII (Resource Acquisition Is Initialization) pattern, I think it can solve resource release issue in the following two situations,

    1. Local function object (on stack);
    2. Object (either on heap or stack) pointer by auto_ptr;

    But it has the limitation that the object pointed by a normal pointer and allocated on heap (using new or new[]) can not be automatically released, either the function returns normally or during exception stack unwinding. Is that correct?


    thanks in advance,
    George

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm not quite sure what you mean?
    RAII is basically a local variable that holds onto a resource (such as a pointer or handle) and releases that resource when the RAII object goes out of scope.

    The simplest pattern for a RAII is:
    Code:
    class RAII
    {
    public:
        RAII( sometype  var ) : m_Var( var ) {}
    
        ~RAII()
        {
            // Do whatever you need to do to release m_Var...
        }
    
    private:
        sometype  m_Var;
    };

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Some things like some smart pointers hold on to a resource until there's no references to it anymore. Typical smart pointer for memory allocated on the heap. It will hold on to the memory as long as you have a smart pointer pointing to it. If none, it will free the memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think George2 means that in some cases destructors are called automatically and in other's not (if you allocate with new/new[] and fail to call delete).

    So it would seem that if you manage your RAII objects through naked, dumb pointers, you are only half-way there.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Heap memory is just another resource that you need to manage using RAII. That's what auto_ptr is for. In a program that really follows the RAII pattern, you'll find that raw pointers are extremely rare.
    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

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Yes, Elysia. I agree smart pointer could solve this issue, in my question, I mean raw pointer. :-)


    Quote Originally Posted by Elysia View Post
    Some things like some smart pointers hold on to a resource until there's no references to it anymore. Typical smart pointer for memory allocated on the heap. It will hold on to the memory as long as you have a smart pointer pointing to it. If none, it will free the memory.

    regards,
    George

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Yes, anon. This is my point.


    How do you think of it?

    Quote Originally Posted by anon View Post
    I think George2 means that in some cases destructors are called automatically and in other's not (if you allocate with new/new[] and fail to call delete).

    So it would seem that if you manage your RAII objects through naked, dumb pointers, you are only half-way there.

    regards,
    George

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your advice, CornedBee!


    Quote Originally Posted by CornedBee View Post
    Heap memory is just another resource that you need to manage using RAII. That's what auto_ptr is for. In a program that really follows the RAII pattern, you'll find that raw pointers are extremely rare.

    regards,
    George

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks cpjust,


    Sorry that I may not make myself understood. I mean if there is a local pointer type variable and it is pointed to heap buffer.

    Example,

    Code:
    int* p = new int[10];
    When there are exceptions in the middle of the function, there will be memory leak (no destructors called and no automatically resource/memory released in RAII pattern). Any comments?

    Quote Originally Posted by cpjust View Post
    I'm not quite sure what you mean?
    RAII is basically a local variable that holds onto a resource (such as a pointer or handle) and releases that resource when the RAII object goes out of scope.

    The simplest pattern for a RAII is:
    Code:
    class RAII
    {
    public:
        RAII( sometype  var ) : m_Var( var ) {}
    
        ~RAII()
        {
            // Do whatever you need to do to release m_Var...
        }
    
    private:
        sometype  m_Var;
    };

    regards,
    George

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Yes, you are correct. Therefore you should always put raw pointers inside a smart pointer to automatically release it if there's an exception.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your advice, Elysia!


    Quote Originally Posted by Elysia View Post
    Yes, you are correct. Therefore you should always put raw pointers inside a smart pointer to automatically release it if there's an exception.

    regards,
    George

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Just make sure you read the documentation about the smart pointer you want to use carefully. For example, std::auto_ptr is fine for pointer to single objects, but it should never be used for arrays of objects because its destructor calls delete, not delete [].

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Example, int* p = new int[10];
    >> When there are exceptions in the middle of the function, there will be memory leak (no
    >> destructors called and no automatically resource/memory released in RAII pattern). Any
    >> comments?

    That is just an example of not using RAII. If you did use RAII, then you would not have that problem.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your advice, cpjust!


    Quote Originally Posted by cpjust View Post
    Just make sure you read the documentation about the smart pointer you want to use carefully. For example, std::auto_ptr is fine for pointer to single objects, but it should never be used for arrays of objects because its destructor calls delete, not delete [].

    regards,
    George

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    I agree, thanks Daved!


    Quote Originally Posted by Daved View Post
    >> Example, int* p = new int[10];
    >> When there are exceptions in the middle of the function, there will be memory leak (no
    >> destructors called and no automatically resource/memory released in RAII pattern). Any
    >> comments?

    That is just an example of not using RAII. If you did use RAII, then you would not have that problem.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting out a resource manager
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 11-10-2008, 06:12 PM
  2. unmanaged resource
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2008, 03:23 AM
  3. CreateProcess with Resource of executable, not the Filename
    By Ktulu in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2006, 12:07 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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