473,395 Members | 1,730 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,395 software developers and data experts.

unmanaged resource

200 100+
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 Initialization) or auto_ptr to wrap it. Is my understanding correct?

Here is a sample about what is unmanaged resource.

--------------------
http://www.gotw.ca/gotw/066.htm

Moral #3: Always perform unmanaged resource acquisition in the constructor body, never in initializer lists. In other words, either use "resource acquisition is initialization" (thereby avoiding unmanaged resources entirely) or else perform the resource acquisition in the constructor body.
--------------------


thanks in advance,
George
Jan 2 '08 #1
6 1190
weaknessforcats
9,208 Expert Mod 8TB
Not true. You use function-try blocks for this.
Jan 2 '08 #2
George2
200 100+
Sorry weaknessforcats, I am confused. My question is what is unmanaged resource and whether my understanding of unmanaged resource is correct. Your reply has relationship with my question? :-)


Not true. You use function-try blocks for this.

regards,
George
Jan 3 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
A resource managed by the compiler is a managed resource.

A resource managed by you is an unmanaged resource.
Jan 3 '08 #4
George2
200 100+
Thanks weaknessforcats,


I think a pointer which is pointed to something allocated with new is unmanaged resource, and if we add a wrapper -- e.g. making the pointer as a member of a class, then the class is managed resource.

I am not sure whether my understanding is correct. It is appreciated if you could provide some samples about the two statement below.

A resource managed by the compiler is a managed resource.

A resource managed by you is an unmanaged resource.

have a good weekend,
George
Jan 4 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
You are making this too hard.

The compiler manages all resources not allocated by new. The resources allocated by new are unmanaged.

Expand|Select|Wrap|Line Numbers
  1. int* data = new int[30];
  2.  
Here data is a pointer to an int. It is a local variable in a stack frame. It is managed by the compiler. When it goes out of scope, the destructor for the pointer will be called. Except in this case there is no destructor for an int* so the compiler just deletes it.

The fact that you did something without cleaning it up is irrelevant.

The int[30] is an unmanaged resource. It is your respnsibility to release it when you are finished with it.

If you put this in a class:
Expand|Select|Wrap|Line Numbers
  1. class George2
  2. {
  3.     int* data;
  4.     public:
  5.     George2() : data(new int[30]) {}
  6.  
  7. };
  8. int main()
  9. {
  10.     George2 obj;
  11.     George2* pobj = new George2;
  12. }
  13.  
Here obj is managed (the compiler allocated it). The fact that it has allocated other resources is irrelevant. When obj goes out of scope, the destructor will be called (except there isn't one) so if any problems arise where data is not delete, it will be your fault.

pobj is a managed resource (the compiler allocated it). the new George2 is unmanaged. When pobj goes out of scope, the pointer is destroyed. Pointers have no destructors so the George2 destrcutor (if there is one) will not be called. You have to delete the object yourself. And even here, you are responsible for cleanup of data members.

And please, do not drag in Microsoft's "managed C++" where they mean code running under the CLR.
Jan 4 '08 #6
George2
200 100+
Thanks weaknessforcats,


Your sample is great! My question is answered.

You are making this too hard.

The compiler manages all resources not allocated by new. The resources allocated by new are unmanaged.

Expand|Select|Wrap|Line Numbers
  1. int* data = new int[30];
  2.  
Here data is a pointer to an int. It is a local variable in a stack frame. It is managed by the compiler. When it goes out of scope, the destructor for the pointer will be called. Except in this case there is no destructor for an int* so the compiler just deletes it.

The fact that you did something without cleaning it up is irrelevant.

The int[30] is an unmanaged resource. It is your respnsibility to release it when you are finished with it.

If you put this in a class:
Expand|Select|Wrap|Line Numbers
  1. class George2
  2. {
  3.     int* data;
  4.     public:
  5.     George2() : data(new int[30]) {}
  6.  
  7. };
  8. int main()
  9. {
  10.     George2 obj;
  11.     George2* pobj = new George2;
  12. }
  13.  
Here obj is managed (the compiler allocated it). The fact that it has allocated other resources is irrelevant. When obj goes out of scope, the destructor will be called (except there isn't one) so if any problems arise where data is not delete, it will be your fault.

pobj is a managed resource (the compiler allocated it). the new George2 is unmanaged. When pobj goes out of scope, the pointer is destroyed. Pointers have no destructors so the George2 destrcutor (if there is one) will not be called. You have to delete the object yourself. And even here, you are responsible for cleanup of data members.

And please, do not drag in Microsoft's "managed C++" where they mean code running under the CLR.

regards,
George
Jan 5 '08 #7

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

Similar topics

15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
6
by: Eric | last post by:
for example: SqlConnection is used in my project, how can I know if all connections were closed after open and execution. If some guys forget to close connections after using, how can i check it...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
3
by: MJS_Jeff | last post by:
Folks, I have posted this message on a different newsgroup but I did not get a single response. I take it that it must not have been the right forum. Anyways. I am seeking the solution to the...
3
by: Steve | last post by:
I have former VC++ 6.0 application which was ported to .NET. Now we continue to develop this app under VS.NET. The app now contains both managed and unmanaged classes. Now I have a problem with...
4
by: Maxwell | last post by:
Hello, Newbie question here for disposing of unmanaged resources in MC++.NET. I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged C++ dll. What I am trying to figure out is...
7
by: thomson | last post by:
Hi All, Connecting to SQL Server we say its an Un Managed connection Can any one tell me whether this statement objConnection = new System.Data.OleDb.OleDbConnection(strConnectionString); ...
5
by: ttc | last post by:
Hi All, I have a managed class that inherits from an unmanged class. The question is, if the object of the manged class get garbage collected, will the memory be free automatically for me or...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.