locked
RAII (Resource Acquisition Is Initialization) pattern limitations RRS feed

  • Question

  • 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

     

    Wednesday, December 26, 2007 7:59 AM

Answers


  • yes you are correct.
    When you use word stack this means you are not using dynamic acclocation .And scope of each thing either function or member variable or class is local When you will come out from a Function and the variable is declared inside the function to after comeing out the variable will automatically free .if it is on class level to When your class will terminate it will clear all the thing.

    auto_ptr :-
                  This is know as a smart pointer .And in this case you should not have to worry regarding allocation and deaalocation of memory your smart pointer will do all the things for you.It ensures that an allocated object is automatically deleted when control leaves a block.Normal Pointer and auto_ptr are two different things.if you are using normal pointer you have to take care of allocation and deallocation of memory .But that is not the limitation of smart pointer it is the limitation of normal pointer.

    Thanx
    Wednesday, December 26, 2007 8:59 AM

All replies


  • yes you are correct.
    When you use word stack this means you are not using dynamic acclocation .And scope of each thing either function or member variable or class is local When you will come out from a Function and the variable is declared inside the function to after comeing out the variable will automatically free .if it is on class level to When your class will terminate it will clear all the thing.

    auto_ptr :-
                  This is know as a smart pointer .And in this case you should not have to worry regarding allocation and deaalocation of memory your smart pointer will do all the things for you.It ensures that an allocated object is automatically deleted when control leaves a block.Normal Pointer and auto_ptr are two different things.if you are using normal pointer you have to take care of allocation and deallocation of memory .But that is not the limitation of smart pointer it is the limitation of normal pointer.

    Thanx
    Wednesday, December 26, 2007 8:59 AM
  •  George2 wrote:

    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?



    I somehow do not fully understand the scenarios above nonetheless, every object certainly has a constructor/destructor that is being called when the object is constructed/destroyed. Thus, contained resources should not leak...essentially this is the whole point of the RAII idiom in the first place.

    Same goes for smart pointers...reuglar pointers are certainly different in the sense that they actually do not follow the RAII idiom in the first place. That was the reason for the standard to provide the 'auto_ptr' as a wrapper around regular pointers.
    Wednesday, December 26, 2007 1:24 PM
  • Thanks for your advice, Pintu!

     

     

    My question is answered.

     

     Pintu Shukla wrote:

    yes you are correct.
    When you use word stack this means you are not using dynamic acclocation .And scope of each thing either function or member variable or class is local When you will come out from a Function and the variable is declared inside the function to after comeing out the variable will automatically free .if it is on class level to When your class will terminate it will clear all the thing.

    auto_ptr :-
                  This is know as a smart pointer .And in this case you should not have to worry regarding allocation and deaalocation of memory your smart pointer will do all the things for you.It ensures that an allocated object is automatically deleted when control leaves a block.Normal Pointer and auto_ptr are two different things.if you are using normal pointer you have to take care of allocation and deallocation of memory .But that is not the limitation of smart pointer it is the limitation of normal pointer.

    Thanx

     

     

    regards,

    George

    Wednesday, December 26, 2007 1:45 PM
  • Thanks Andreas,

     

     

    So you mean if we use normal pointer to allocate resources on heap, we do not follow RAII pattern, right? If yes, I can understand that in this situation, it is out of the scope of RAII (not covered in RAII) and it should not be a limitation of RAII.

     

     Andreas Masur wrote:

     George2 wrote:

    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?



    I somehow do not fully understand the scenarios above nonetheless, every object certainly has a constructor/destructor that is being called when the object is constructed/destroyed. Thus, contained resources should not leak...essentially this is the whole point of the RAII idiom in the first place.

    Same goes for smart pointers...reuglar pointers are certainly different in the sense that they actually do not follow the RAII idiom in the first place. That was the reason for the standard to provide the 'auto_ptr' as a wrapper around regular pointers.

     

     

    regards,

    George

    Wednesday, December 26, 2007 1:47 PM
  •  George2 wrote:

    So you mean if we use normal pointer to allocate resources on heap, we do not follow RAII pattern, right? If yes, I can understand that in this situation, it is out of the scope of RAII (not covered in RAII) and it should not be a limitation of RAII.



    Yes that was pretty much my point...RAII is based on objects and the availibility of constructors/destructors.
    Wednesday, December 26, 2007 2:04 PM
  • Thanks for your confirmation, Andreas!

     

     

    My question is answered.

     

     Andreas Masur wrote:

     George2 wrote:

    So you mean if we use normal pointer to allocate resources on heap, we do not follow RAII pattern, right? If yes, I can understand that in this situation, it is out of the scope of RAII (not covered in RAII) and it should not be a limitation of RAII.



    Yes that was pretty much my point...RAII is based on objects and the availibility of constructors/destructors.

     

     

    regards,

    George

    Wednesday, December 26, 2007 2:15 PM