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

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
deterministically 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 2440
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
deterministically 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() { CloseMyResources(); } // 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.Dispose() { } // 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.IDisposable.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
deterministically 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() { CloseMyResources(); } // 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,
IMyRestrictedDispose, 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->MyRestrictedCleanup(); }
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
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...
4
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
by: kchalla | last post by:
Can anybody let me know the difference between public and private class access modifiers? Thanks in advance
6
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...
0
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...
2
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...
4
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...
6
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...
5
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
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...

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.