Re: RAII (Resource Acquisition Is Initialization) pattern limitati



Thanks David,


It is my mistake to misunderstand RAII pattern that it covers new/new[]
before. Now my question is answered.


regards,
George

"David Wilkinson" wrote:

George wrote:
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?

George:

If you use new or new [] without std::auto_ptr or std::vector then you
are not using RAII, and you have the potential for memory leaks. This is
not a "limitation" of RAII.

--
David Wilkinson
Visual C++ MVP

.



Relevant Pages

  • Re: Is this good style of C++?
    ... >> requires that acquired resource should be released. ... it is not easy to cause memory leak. ... this rule is more strict than RAII. ... pointer will not be released by releasing the pointer. ...
    (comp.lang.cpp)
  • Re: RAII (Resource Acquisition Is Initialization) pattern limitations
    ... 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, ... 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 can not be automatically released, either the function returns normally or during exception stack unwinding. ...
    (microsoft.public.vc.language)

Loading