VS 2005: Dispose method Reserved

G

Guest

All -
As we all know, Dispose method is reserved in C++ 8 and the expected syntax
is to use ~MyClass().

In 1.1, we used to have following structure for Dispose
[Code illustrated in C# for brevity:)]
class MyClass : IDisposable
{
public void Dispose() { Dispose(true); GC.Supressxxx(this); }
~Dispose() { Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing) { xxx } // Disposing ilustrates if we are in finalizer
yyyy
}
}

System.Windows.Forms.Form used to have such a model, and we used it for our
finalizable classes to implement all cleanups in the single "protected
override Dispose(bool disposing)", which nicely has the "disposing" parameter.

Now C++ 8.0, disallows having any function by name Dispose and the structure
has to be changed with a new name (protected virtual "DisposeObject" for
instance)

The question is -
Why does the compiler forbids even overloaded Dispose [like Dispose(bool)]
methods. I can understand the reserved method for Dispose() but why for
overloads even?

If we simply re-compile our code with the new compiler (after syntax
changes), it we will break our clients. Clients will have to change their
code for overriding the Dispose method!

Any suggestions? [Using VS 2005 Beta 2]
Thanks in advance.

Regardz
Grafix.
 
J

Jochen Kalmbach [MVP]

Hi Grafix!
As we all know, Dispose method is reserved in C++ 8 and the expected syntax
is to use ~MyClass().

In 1.1, we used to have following structure for Dispose
[Code illustrated in C# for brevity:)]
class MyClass : IDisposable
{
public void Dispose() { Dispose(true); GC.Supressxxx(this); }
~Dispose() { Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing) { xxx } // Disposing ilustrates if we are in finalizer
yyyy
}
}

See: Destructors and Finalizers in Visual C++
http://msdn2.microsoft.com/en-us/library/ms177197

In short:
The Dispose-Pattern in 1.1 mapps to:

void Dispose(bool disposing) {
if (disposing) {
~T();
} else {
!T();
}
}

So you just need to implement ~T and !T and you have the desired behavior...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

Jochen -
If an assembly compiled with VC 2005 must be consumed by a C# assembly,
then how can they override the "Dispose(bool disposing)" method, becoz the
method "Dispose(bool)" is forbidden by C++?

My point is that if a (ref) class written in C++ , has both a finalizer and
a dispose method (for deterministic destruction), and a derived class (in
another language like C#) wanted to consume it, then overriding Dispose(bool
disposing) shows if we are in finalizer or not nicely, for the derived class.

C# client of MyClass

class CSharpClass : MyClass (see MyClass from OrigPosting sample)
{
protected override void Dispose(bool disposing)
{
if(disposing) // Got to know if we are disposing or not w.o any effort
{
}

base.Dispose(disposing);
}

Now the C# class has found out if it is in finalizer or not *without* doing
any effort, by examinin the "disposing" parameter. C++ 2005 strips this
facility by making Dispose(bool) reserved!


Regardz
Grafix.

Jochen Kalmbach said:
Hi Grafix!
As we all know, Dispose method is reserved in C++ 8 and the expected syntax
is to use ~MyClass().

In 1.1, we used to have following structure for Dispose
[Code illustrated in C# for brevity:)]
class MyClass : IDisposable
{
public void Dispose() { Dispose(true); GC.Supressxxx(this); }
~Dispose() { Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing) { xxx } // Disposing ilustrates if we are in finalizer
yyyy
}
}

See: Destructors and Finalizers in Visual C++
http://msdn2.microsoft.com/en-us/library/ms177197

In short:
The Dispose-Pattern in 1.1 mapps to:

void Dispose(bool disposing) {
if (disposing) {
~T();
} else {
!T();
}
}

So you just need to implement ~T and !T and you have the desired behavior...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

Curiously enough, you can use "Dispose(Boolean...".
For some reason the compiler reserves "Dispose(bool...)", but not
"Dispose(Boolean...)", so there's your work-around.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
J

Jochen Kalmbach [MVP]

Hi Grafix!
If an assembly compiled with VC 2005 must be consumed by a C# assembly,
then how can they override the "Dispose(bool disposing)" method, becoz the
method "Dispose(bool)" is forbidden by C++?

I don´t understand your problem...
For example, the following C++/CLI class:

public ref class ManagedClass
{
public:
ManagedClass()
{
}
~ManagedClass()
{
}
!ManagedClass()
{
}
};

will be represented in C# as:


public class ManagedClass : IDisposable
{
public ManagedClass();
private void ~ManagedClass();
private void !ManagedClass();
public sealed override void Dispose();
protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool);
protected override void Finalize();
}

So every C# class derived from ManagedClass can override "Dispose(bool)"...

Or what is your problem?

My point is that if a (ref) class written in C++ , has both a finalizer and
a dispose method (for deterministic destruction), and a derived class (in
another language like C#) wanted to consume it, then overriding Dispose(bool
disposing) shows if we are in finalizer or not nicely, for the derived class.

And this is possible... or maybe I misunderstood your problem...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

According to the original post:
"If we simply re-compile our code with the new compiler (after syntax
changes), it we will break our clients. Clients will have to change their
code for overriding the Dispose method"

Using "Boolean" instead of "bool" will mean that you don't break your
clients' code.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
G

Guest

Actually, clients that are overriding Dispose(bool) will be trivially broken
since they'll have to change their overrides to Dispose(Boolean).
But certainly clients' code that calls Dispose(bool) will not be broken
(since bool is identical to Boolean).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
J

Jochen Kalmbach [MVP]

Hi David!
According to the original post:
"If we simply re-compile our code with the new compiler (after syntax
changes), it we will break our clients. Clients will have to change their
code for overriding the Dispose method"

No. The clients will not be broken, because Dispose(bool) can be
overriden... so I do not see any problems.
Using "Boolean" instead of "bool" will mean that you don't break your
clients' code.

If you change the parameter of the method, then it will be broken,
because no client will overload the correct function...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

Dispose(bool) can't be overridden in C++/CLI because the compiler won't let
you have Dispose(bool) in the first place. That's the first issue of the
original poster - the work-around is to use Dispose(Boolean).

The original posters second issue is that clients' code will be broken - the
clients' override of Dispose(bool) will be broken since they'll have to now
override Dispose(Boolean), but at least calls to Dispose(bool) won't be
broken.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
J

Jochen Kalmbach [MVP]

Hi David!
Dispose(bool) can't be overridden in C++/CLI because the compiler won't let
you have Dispose(bool) in the first place.

Yes. You do not need to overload it, just implement ! and ~
I thougt the OP was about deriving in C# (or any other language)...


And if you port your code from MC++ to C++/CLI you need to do many
things so this is not the main part...
And if you use "oldsyntax" then you can use "Dispose"..

So again, I do not see a problem...

That's the first issue of the
original poster - the work-around is to use Dispose(Boolean).

The original posters second issue is that clients' code will be broken - the
clients' override of Dispose(bool) will be broken since they'll have to now
override Dispose(Boolean), but at least calls to Dispose(bool) won't be
broken.

As I said: Bad idea... and I still see not the point of the problem...


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

David -
Thanks - You have understood my problem correctly.
But your solution doesnt wok either -
I tried to change the signature of my method to
virtual void Dispose(Boolean disposing) and still the compiler cries.

But i am using Beta 2. What version are u using? RTM?
If it works in RTM, then i have to move to that.
Thanks for the solution.

Regardz
Natraj.
 
G

Guest

Did you add "override"? Using beta 2, the following works:
virtual void Dispose(Boolean disposing) override
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
J

James Park

Grafix said:
The question is -
Why does the compiler forbids even overloaded Dispose [like Dispose(bool)]
methods. I can understand the reserved method for Dispose() but why for
overloads even?

Because the Dispose(bool) method is modeled by ~T and !T, and the compiler
is trying to prevent you from shooting yourself in the foot.
If we simply re-compile our code with the new compiler (after syntax
changes), it we will break our clients. Clients will have to change their
code for overriding the Dispose method!

No they won't, because the Dispose method will be there to be overridden so
long as you implement ~T and/or !T.

Example:
C++/CLI:
public ref class Class1
{
~Class1() { }
!Class1() { }
};

C#:
class Class2 : Class1
{
protected override void Dispose(bool disposing) { } // compiles fine!
}
Any suggestions? [Using VS 2005 Beta 2]

Reread Jochen posts since he has responded correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top