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

VS 2005: Dispose method Reserved

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.
Dec 26 '05 #1
13 1490
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/
Dec 26 '05 #2
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 [MVP]" wrote:
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/

Dec 26 '05 #3
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

"Grafix" wrote:
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.

Dec 27 '05 #4
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/
Dec 27 '05 #5
Hi David!
so there's your work-around.


Workaround for what? I don´t see a problem...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Dec 27 '05 #6
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

"Jochen Kalmbach [MVP]" wrote:
Hi David!
so there's your work-around.


Workaround for what? I don´t see a problem...

--
Greetings
Jochen

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

Dec 27 '05 #7
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

"Jochen Kalmbach [MVP]" wrote:
Hi David!
so there's your work-around.


Workaround for what? I don´t see a problem...

--
Greetings
Jochen

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

Dec 27 '05 #8
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/
Dec 27 '05 #9
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

"Jochen Kalmbach [MVP]" wrote:
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/

Dec 27 '05 #10
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/
Dec 27 '05 #11
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.

"David Anton" wrote:
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

"Jochen Kalmbach [MVP]" wrote:
Hi David!
so there's your work-around.


Workaround for what? I don´t see a problem...

--
Greetings
Jochen

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

Dec 27 '05 #12
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

"Grafix" wrote:
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.

"David Anton" wrote:
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

"Jochen Kalmbach [MVP]" wrote:
Hi David!

> so there's your work-around.

Workaround for what? I don´t see a problem...

--
Greetings
Jochen

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

Dec 27 '05 #13
"Grafix" <Gr****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
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.
Dec 27 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
9
by: Hasani \(remove nospam from address\) | last post by:
I was reading a ppt ( http://www.gotdotnet.com/team/pdc/4064/tls310.ppt ) and came aross this statement. "Users can leverage a destructor. The C++ compiler generates all the Dispose code...
2
by: Val3 | last post by:
Hi all. I need to build dll(s) and windows services using VB .NET 2005 Express. When I make File/New project the windows contain only Windows application, Windows control library, Console...
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
2
by: evle | last post by:
haw to read data from an Infrared Infrared Remote Control
3
by: Victory | last post by:
Hi, I just used VS2008 and migrated a project from 2005 to 2008. The reason is that MSDN docs is saying the Image object in .NET Framework 3.5 has a Finalize method, which i want to try out since...
4
by: AWW | last post by:
XP VB 2005 drawing line graphics in a PictureBox - from a simple minded former FORTRAN programmer. The help examples have the graphics drawing code (lines & rectangles) in the PictureBox paint...
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...
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
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
Oralloy
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,...
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
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...
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.