473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access modifiers do not affect destructor visibility in ref class?

This compiles and runs successfully in VS2005:

ref class A
{
private:
~A() { this->!A(); } // private!
!A() { } // private!
};

....

A^ a1 = gcnew A();
a1->~A(); // I would expect this to signal a compile
// error regarding accessibility
delete a1; // As well as this.

A a2; // As well as this.
a2.~A(); // And as well as this.

So, given that access modifiers don't have any effect on the visibility on
destructors in ref classes, how would one prevent a client from
deterministical ly cleaning up a managed reference object?

Another thing I find peculiar is that you can call the destructor on an
interface class, even though the compiler enforces the fact that an interface
class cannot have a destructor.

interface class B { };
ref class C : B { };

....

B^ b = gcnew C();
b->~B(); // Compiles fine???
delete b; // this too?

I have a situation where I want to prevent clients from deterministic
destruction of certain objects. For example, let's say I return a special
"read-only" interface to an internal object. I shouldn't be able to destroy
the object via the interface (by calling "delete" or explicitly calling the
destructor as demonstrated above) as this would break encapsulation. I also
don't want to return a copy of the internal object as it might incur a major
performance penalty. I would enforce this in standard C++ by returning a
reference to a const object. How would one recommend I do it in CLI?
Jun 12 '06 #1
3 2465
RitualDave wrote:
So, given that access modifiers don't have any effect on the visibility on
destructors in ref classes, how would one prevent a client from
deterministical ly cleaning up a managed reference object?


There is no such thing as a destructor in .NET. When you define a
destructor in your class, it compiles to something like this (not valid
C++/CLI code, just to illustrate what's going on inside):

ref class A : public System::Object, public IDisposable
{
public:
virtual void Dispose() override
{
}
};

As you see, it's a public function, and even if you tried to make it
private, it would be available via the IDisposable interface. You can't
hide that.

Also, the call to delete does nothing more than calls Dipose(). It
doesn't deallocate any memory.

The solution to your problem is to not define a destructor at all. If
you don't want the d'tor to be called, just don't define it. Then your
class won't be disposable, and no one will be able to call Dispose on
it. If someone calls the delete operator on it, it will do absolutely
nothing.

ref class A
{
private:
!A() { CloseMyResource s(); } // you still need a finalizer
};

A^ a = new A;
delete a; // valid, but this line does absolute nothing, it's a NOP

Tom
Jun 12 '06 #2

Thanks for your reply, Tom.

I'm familiar how destructors and finalizers map to the .NET Dispose pattern.
What I'm attempting to illustrate is that the compiler does not prevent me
from putting a destructor and finalizer under a private block, which mislead
me into thinking that I could limit deterministic destruction by limiting
access to the destructor. I feel that if the compiler allows me to do it,
then it should enforce visibility rules regardless of what IL it produces in
the back end.

Having said that, I just took a look at the C++/CLI specification to see if
it says anything on the matter. Under section 19.13.1 reads: The
access-specifier of a destructor in a ref class is ignored. I guess that
answers that. :) I would suggest that the compiler present an error (or
possibly a warning) when a destructor is defined under a non-public access
modifier in a ref class. C# enforces this by not allowing you to change the
access level of an interface method:

class A
: IDisposable
{
private void IDisposable.Dis pose() { } // error CS0106: The modifier
'private'
// is
not valid for this item

// or

private virtual void Dispose() { } // error CS0621: virtual or
abstract
// members
cannot be private

// or

protected virtual void Dispose() { } // error CS0536: 'B' does not
implement
// member
System.IDisposa ble.Dispose().
//
'B.Dispose()' is either static, not public,
// or has a
wrong return type.
}
As for the solution you presented regarding limiting the deterministic
destruction of an object... I would agree with you that if I wanted to
prevent EVERYONE from calling Dispose() on my object, I should just not
define the destructor. However, I'm looking to just RESTRICT who can do it
to enforce encapsulation without having to implement a wrapper class, perform
an expensive copy, or rely on documentation to enforce a policy.

"Tamas Demjen" wrote:
RitualDave wrote:
So, given that access modifiers don't have any effect on the visibility on
destructors in ref classes, how would one prevent a client from
deterministical ly cleaning up a managed reference object?


There is no such thing as a destructor in .NET. When you define a
destructor in your class, it compiles to something like this (not valid
C++/CLI code, just to illustrate what's going on inside):

ref class A : public System::Object, public IDisposable
{
public:
virtual void Dispose() override
{
}
};

As you see, it's a public function, and even if you tried to make it
private, it would be available via the IDisposable interface. You can't
hide that.

Also, the call to delete does nothing more than calls Dipose(). It
doesn't deallocate any memory.

The solution to your problem is to not define a destructor at all. If
you don't want the d'tor to be called, just don't define it. Then your
class won't be disposable, and no one will be able to call Dispose on
it. If someone calls the delete operator on it, it will do absolutely
nothing.

ref class A
{
private:
!A() { CloseMyResource s(); } // you still need a finalizer
};

A^ a = new A;
delete a; // valid, but this line does absolute nothing, it's a NOP

Tom

Jun 12 '06 #3
RitualDave wrote:
I would agree with you that if I wanted to
prevent EVERYONE from calling Dispose() on my object, I should just not
define the destructor.
Having a strong C++ background, I understand your problem, but C++/CLI
is not exactly C++. It has to follow the rules of the CLI closely. I'm
not an authority, but I think the .NET framework insists that Dispose be
public, so you can not restrict it. Even if you could do that, that
restriction would be half baked, as Dispose would still be publicly
available via IDisposable.

I agree that the compiler should show at least a warning message
pointing out that destructors for ref classes must be public.
However, I'm looking to just RESTRICT who can do it
to enforce encapsulation without having to implement a wrapper class, perform
an expensive copy, or rely on documentation to enforce a policy.


As a workaround, you could create your own interface,
IMyRestrictedDi spose, where Dispose is defined as protected, for
example. The compiler is just not going to associate that with the
concept of destructors. Deterministic destruction with the stack syntax
is only possible if the destructor is public. You can, however, still
use the RAII concept with a helper class to call your restricted cleanup
in a deterministic way:

ref class Guard
{
public:
Guard(A^ source) : a(source) { }
~Guard() { a->MyRestrictedCl eanup(); }
private:
A^ a;
};

Tom
Jun 13 '06 #4

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

Similar topics

2
1489
by: Terje Slettebø | last post by:
Well, as is customary when you're new to a group, you tend to post quite a bit, :) so here's one more. Some things I've been wondering about with PHP (5). Today, I worked on an implementation of a finite state machine. Unlike the pear::fsm, this one supports hierarchical states, and I intend to make it available, as well. It exists in both PHP 4 and 5 versions, but only the PHP5 version is relevant for this example. The base class...
4
2215
by: Laurence Nuttall | last post by:
What is the modifiers property of a label mean? I looked in the help, but couldn't find any explantion for it. Thanks in Advance, Laurence Nuttall Programmer Analyst III
3
1748
by: kchalla | last post by:
Can anybody let me know the difference between public and private class access modifiers? Thanks in advance
6
4751
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
0
3730
by: TheCoder | last post by:
I am making a D-base with web conectivity for my class project. I have everything working but the subit button sends the data to the correct fields but afterwards it wants to reproduce new blank text boxes how do I stop this and how would I code it to redirect to a thank you page. All with out the help of a web server this has to br ran from the desktop and with in IE with a local directory. Pleas help I am stuck. here is the code! ...
2
2741
by: Bob Weiner | last post by:
Is it possible to create a property and specify different access modifiers for the get and set methods? From the property definition it appears that you can define attributes but not access modifiers. I have never used attributes so I don't know if they are useful for this purpose. What I want to do is to create an 'Entry' class that will be used by two other classes. One class will create the 'Entry', assign limits, a default value...
4
3210
by: Picho | last post by:
Hi all, Using reflection, I can invoke/call private methods of an object. is this intended? if yes, why? in what scenario (example would be good) should I be givven the option to use something that was declared private (not public) and not mine to use? seems to me this is somewhat malicious... if not.... well....
6
8613
by: Davids | last post by:
having a 100% working aspx page I wanted to create a user control from a small DataList section (which has an ID="DataList1") of the page, so I created an ascx file with the DataList and Register the control as should be done. Now the DataList1 can't be found any more: The name 'DataList1' does not exist in the current context why's that??
5
389
by: Roy Gourgi | last post by:
Hi, Is it possible to declare a variable or array in the class that is accessible to all the methods in the class? For the example below, I would like to make the variable lnVar1 accessible to both methods m1 and m2 but I am getting an error. Is there another way to do this? Thanks Roy
0
8984
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
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6793
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
6073
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
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
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.