473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

RAII (Resource Acquisition Is Initialization) pattern limitations

200 100+
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
Dec 26 '07 #1
3 1211
weaknessforcats
9,208 Expert Mod 8TB
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,
I'm not sure what you mean here. All heap allocations are released as part of stack unwinding and that includes calling destrcutors on all array elements.

That said, heap allocations in the current stack frame are not released. You need to catch the exception inside the function doing the throw, delete the heap allocations made in that function and rethrow the exception.

Bear in mind that a pointer cannot be deleted unless you are deleting the final copy of it. If you don't know this, then you can't delete. Here you use a reference counted handle.

Beware of auto_ptr. Generally, it does not work as expcected. See Effective STL by Scott Meyers.
Dec 26 '07 #2
George2
200 100+
Hi weaknessforcats,


Two more comments,

1.

[quote=weaknessforcats]I'm not sure what you mean here. All heap allocations are released as part of stack unwinding and that includes calling destrcutors on all array elements.[quote=weaknessforcats]

Suppose there is code like this,

Expand|Select|Wrap|Line Numbers
  1. void f()
  2. {
  3.     int* p = new int[10];
  4.  
  5.     // exception here
  6. }
  7.  
How will the memory allocated and pointed by p released automatically during stackl unwinding? We have to explicitly delete it in exception handler -- and RAII pattern does not use the explicit catch block at all. :-)

2.

Beware of auto_ptr. Generally, it does not work as expcected. See Effective STL by Scott Meyers.
Sorry that I do not have the book at hand. Could you show some major ideas and insights please?


regards,
George
Dec 27 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
No amount of comments by me is going to be satisfactory here. You need to read the statements made by other professionals. To develop in C++ you absolutely need textbook references.
Dec 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Pierre Rouleau | last post by:
As much as I love Python, I sometimes find myself wishing Python supported the RAII idiom (resource acquisition is initialization) that is available in C++, the emerging D language, and others. ...
26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
11
by: yang su | last post by:
I have a very simple program listed below, which basically tries to use resources but prevent from memory leak should exception occurs. class T is the resource and class User is to use the...
4
by: MikeB | last post by:
Hi, Recently I was asked to look at some code where RAII is used to ensure automatic cleanup of a resource. Unfortunately, cleaning up the resource requires that the destructor make a call...
23
by: Markus Elfring | last post by:
The class "auto_ptr" implements the RAII pattern for pointer types. It seems that an implementation is not provided for non-pointer values by the STL so far. I imagine to use the "acquisition" for...
5
by: Nick Keighley | last post by:
Hi, In the beginning the code was:- BRUSHH new_brush = create_brush() BRUSHH old_brush = select_brush (new_brush) draw(...) select_brush (old_brush) destroy_brush (new_brush)
6
by: tomthemighty | last post by:
A simple RAII wrapper for acquiring a Thingy that has two alternative acquire methods, acquire() and acquireAndFurtle(): class ThingyAcquirer : private boost::noncopyable { public: explicit...
1
by: George2 | last post by:
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...
1
by: George2 | last post by:
Hello everyone, I think unmanaged resource means the resources (e.g. memory and file handler) which is used directly (new, FILE*) other than using a wrapper class (Resource Acquisition Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.