One item that has not been talked of so far is using the idea of
"registered owners" to go back and do reference counting.
Means:
(a) you use an "extended Idisposable the way outlined in this thread.
(b) you use AddRef / Release calls (possibly automated, maybe with
pointers back for debugging [who is the owner f this ***** resource?])
(c) the disposable interface will auto-dispose when the number of
registered owners goes down to 0 (not the initial 0).
Note that this proposal is ortogonal to garbage collection. It would
just, over the proposed solution, have the advantagaes that:
(a) you could have multiple owners.
(b) you could easily transfer ownership (adding second owner, releasing
first).
(c) it would IMHO not interfere with GC - actually when the object gets
gc'd and still has registered owners (programmer bug), then the
automatic dispose would be called.
Thats the core point I wonder about, btw - why have people not put
reference counting (for resources) ON TOP of GC. Let the GC handle the
garbage, and let the reference counter handle the "Idispoable" pattern.
Add this to "smart templates" for variables and you are gone.
BASICALLY - have every reference variable referencing an Idisposable
implementing style call the correct addref/release functions on the
Idisposable method (compiler injection) and you are combining both
advantages.
Regards
Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)
--
Still writing SQL and dealing with DataSets?
Why dont you use our EntityBroker.
The most advanced o/r and business object toolkit in town.
-----Original Message-----
From: Phil Parker [mailto:parker@;SENSORMATIC.COM]
Sent: Mittwoch, 6. November 2002 07:17
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Enforcing IDisposable ownership rules
The syntax is admittedly ugly, though maybe with some work it could be
acceptable. One change which might help would be to have the compiler
enforce a rule that ownership must be assigned in each and every
constructor for a disposable class. This rule would be enforced instead
of alteration to the new operator.
Also, instead of two attributes (disposable and disposableowner), only
one is really needed, so that:
[disposable()]
class MyResource
{
File m_file;
// following causes compiler to generate an error as it does not
assign ownership to the created instance
public MyResource()
{
}
// following causes compiler to generate an error as it does not
assign ownership to the created instance
public MyResource(IDisposable owner)
{
}
// following is ok
public MyResource(IDisposable owner)
{
AssignOwner(owner);
}
public void OnDispose()
{
m_file.close();
}
}
Now, when using the disposable resource, the syntax becomes a little
cleaner. Still not great - I've yet to come up with an alternative to
the addition of a scope keyword, which creates a hidden instance of a
'Scope' class.
[disposable()]
class MyExample
{
MyResource m_resource;
public MyExample(IDisposable owner)
{
m_resource = new MyResource(this);
AssignOwner(owner);
}
public void Example1()
{
MyResource firstResource = new MyResource(localscope);
scope(myScope)
{
MyResource resource = new MyResource(myScope);
}
}
}
On Tue, 5 Nov 2002 17:00:14 -0800, Chris Sells
<[EMAIL PROTECTED]>
wrote:
>In spite of the DF Police, I love the idea of providing and enforcing
>disposable object ownership rules. However, what you're proposing (even
>as supported by a language directly) has a syntax so heavy-duty as to
>be prohibitive, both to understand and to use. Having said that, I
>don't have anything better...
>
>Chris Sells
>http://www.sellsbrothers.com
>http://www.develop.com
You can read messages from the Advanced DOTNET archive, unsubscribe from
Advanced DOTNET, or subscribe to other DevelopMentor lists at
http://discuss.develop.com.
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.