473,796 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unmanaged resource

200 New Member
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 1209
weaknessforcats
9,208 Recognized Expert Moderator Expert
Not true. You use function-try blocks for this.
Jan 2 '08 #2
George2
200 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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
11790
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 performs the query is contained in a delegate function that I execute via a second thread. On 1 or 2 of the 600+ servers the query hangs. I've tried to use Thread.Join() coupled with a Thread.Abort() but this does not kill the thread. Based on...
5
2109
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 problems I am employing the aforementioned software in trying to track down the problems. After an hour or so of program execution, a snapshot of the managed heap shows 27,025 BinaryReader objects as having been garbage collected but never...
6
1803
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 out ? best, eric
4
40037
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 deals with managed in one place, and code that deals with unmanaged in another place, but I can't seem to find anything that clearly explains what that means. I think if I used a Windows API function to optain a handle, that handle would be an...
3
1157
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 classic ReadersWritesLock with a slight twist in My unmanaged C++. I can not import "System" and "System.Threading" namespaces. I need to use some kind of lock (semaphore, Mutex, or any other alternative), among threads to alow multiple threads...
3
2651
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 usage of resources in managed classes. For example I want to display warning message and want to load that warning message from the resource file. The problem is that in this mixed managed/unmanaged project I don't have pure managed resources...
4
2085
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 what is the "best practice" for disposing of pointers to unmanaged classes that you have newed in your constructor in MC++ For a better description of what is the standard affair I have tried looking online at:
7
1564
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); is a call to an unmanaged resource
5
3902
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 only the bit that is used by the managed code? What about the unmanaged bit? Does anyone know of any article that discuss about this? Any help is appreciated.
1
1852
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 Initialization) or auto_ptr to wrap it. Is my understanding correct? Here is a sample about what is unmanaged resource.
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.