If you have a class that manages a resource (or something similar such as an
unmanaged pointer), and you implement a Dispose( ) method, are you mandating
to the users of your assembly that they *must* use this method to clean up
(or endure leaks or whatever)? Does this become one of their contractual
obligations in using this assembly?
Are are you simply saying to developers: "Because this class manages a
resource, Dispose( ) gives you the opportunity to force this object to
relinquish its resources at an explicit point." In other words, if the user
*does not* call Dispose( ), then the finaliser should still (at some point)
take care of cleaning up the resources, and it is *legal* for a program to
be written this way?
The reason I ask is that we have a quite complicated framework of Managed
objects (actually written in Managed C++) that control unmanaged resources
(native C++ objects). The key complication is that there are dependencies
amongst the unmanaged resources.
For example, a UEntity unmanaged object requires a USession unmanaged object
in order to communicate with an application server over TCP/IP,. The UEntity
unmanaged object is controlled by a managed Entity object, and the USession
object is controlled by a managed Session object. Naturally enough when
Entity finalises, it will release its corresponding resources, with same
said for the Session object.
The nett result is that, without convoluted handling code, it is entirely
possible for Session to finalise *before* Entity. The finaliser for Session
would in turn clean up USession.
The finaliser for Entity is now stuck between a rock and a hard place. It
needs USession to ensure it can communicate something back to the
application server, yet USession has been destroyed.
Has anyone come across this kind of situation before? It would seem to me
that the only practical way of ensuring that the dependencies are retained
is to use reference-counted pointers for the unmanaged resources.
That way Session holds 1 reference to the USession object, and Entity holds
1 reference. USession only "self-destructs" if the reference count drops to
zero, so a finalise of Session ahead of USession would reduce the reference
count but not result in object destruction of USession because Entity is
still holding a reference.
If anyone has solved this in any other different/lateral/imaginative way I'd
be pleased to hear about it.
Thanks
Kevin 1 990
Kevin Frey wrote: If you have a class that manages a resource (or something similar such as an unmanaged pointer), and you implement a Dispose( ) method, are you mandating to the users of your assembly that they *must* use this method to clean up (or endure leaks or whatever)? Does this become one of their contractual obligations in using this assembly?
You're at least saying "it'd be a really good idea for you to call Dispose
on this object when you're done with it, or something expensive is likely to
be wasted."
Are are you simply saying to developers: "Because this class manages a resource, Dispose( ) gives you the opportunity to force this object to relinquish its resources at an explicit point." In other words, if the user *does not* call Dispose( ), then the finaliser should still (at some point) take care of cleaning up the resources, and it is *legal* for a program to be written this way?
Yes it's legal (if unadvisable), and the finalizer should take care. The
preferred pattern for implenting IDisposable is this (C# syntax, sorry):
class C : IDisposable
{
private void IDisposable.Dispose()
{
Dispose(true);
}
~C()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// release managed resources
GC.SuppressFinalize(this);
}
// release unmanaged resources
}
} The reason I ask is that we have a quite complicated framework of Managed objects (actually written in Managed C++) that control unmanaged resources (native C++ objects). The key complication is that there are dependencies amongst the unmanaged resources.
For example, a UEntity unmanaged object requires a USession unmanaged object in order to communicate with an application server over TCP/IP,. The UEntity unmanaged object is controlled by a managed Entity object, and the USession object is controlled by a managed Session object. Naturally enough when Entity finalises, it will release its corresponding resources, with same said for the Session object. The nett result is that, without convoluted handling code, it is entirely possible for Session to finalise *before* Entity. The finaliser for Session would in turn clean up USession.
The finaliser for Entity is now stuck between a rock and a hard place. It needs USession to ensure it can communicate something back to the application server, yet USession has been destroyed.
It's important that the finalizer for Entity not try to do anything other
than clean up resources. That probably includes attempting to communicate.
If you have a "close" protocol, let Dispose() initiate that, but just drop
the connection if you get to the finalizer.
Has anyone come across this kind of situation before? It would seem to me that the only practical way of ensuring that the dependencies are retained is to use reference-counted pointers for the unmanaged resources. That way Session holds 1 reference to the USession object, and Entity holds 1 reference. USession only "self-destructs" if the reference count drops to zero, so a finalise of Session ahead of USession would reduce the reference count but not result in object destruction of USession because Entity is still holding a reference.
If anyone has solved this in any other different/lateral/imaginative way I'd be pleased to hear about it.
You may indeed need to use reference counting on the unmanaged objects if
your UEntity has a direct relationship with a USession that's managed by a
different object, just as you would in a pure unmanaged case if a single
object (USession) has two "owners".
-cd This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter Oliphant |
last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have
a class derived from Form that creates an instance of one of my custom
classes via gcnew and stores the pointer in a...
|
by: Smokey Grindle |
last post by:
I have a list box on my form, but I need to databind it to a data table that
is a private member of the form's class... so I basically have
Public Class MyForm
priate m_MyTable as new datatable...
|
by: Johnny E. Jensen |
last post by:
Hellow
I'am not sure what to think about the Garbage Collector.
I have a Class OutlookObject, It have two private variables.
Private Microsoft.Office.Interop.Outlook.Application _Application =...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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...
|
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...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
| | |