473,796 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

invoking destructor from within instance method

Is it legal to invoke my destructor from within an instance function?

For example,
bool MyClass::someFu nction() {
SomeOtherClass: someFunction(th is);
// No other references to 'this'
// after this point
return true;
}

SomeOtherClass: :someFunction(M yClass m) {
delete m;
}

--
Fred Kleinschmidt
Jun 27 '08 #1
6 1734
Fred wrote:
Is it legal to invoke my destructor from within an instance function?

For example,
bool MyClass::someFu nction() {
SomeOtherClass: someFunction(th is);
// No other references to 'this'
// after this point
return true;
}

SomeOtherClass: :someFunction(M yClass m) {
delete m;
}

It's legal. We use this with smart pointers.

When the reference count for the object goes to zero, the object deletes
itself. If I can get the syntax right...

void smartTarget::re lease()
{
if (--refcount == 0)
delete this;
}

smartPointer::~ smartPointer
{
m_Target->release();
}

Looks odd I admit, and you'd better not access any class data after the
delete!

Andy
Jun 27 '08 #2
On Jun 3, 6:01 am, Fred <fred.l.kleinsc hm...@boeing.co mwrote:
Is it legal to invoke my destructor from within an instance function?

For example,
bool MyClass::someFu nction() {
SomeOtherClass: someFunction(th is);
// No other references to 'this'
// after this point
return true;

}

SomeOtherClass: :someFunction(M yClass m) {
delete m;

}

--
Fred Kleinschmidt

Unsafe.

Myclass m;
m.someFunction( ); // delete variable on stack!
Jun 27 '08 #3
Joshua wrote:
On Jun 3, 6:01 am, Fred <fred.l.kleinsc hm...@boeing.co mwrote:
>Is it legal to invoke my destructor from within an instance function?

For example,
bool MyClass::someFu nction() {
SomeOtherClass: someFunction(th is);
// No other references to 'this'
// after this point
return true;

}

SomeOtherClass ::someFunction( MyClass m) {
delete m;

}

--
Fred Kleinschmidt


Unsafe.

Myclass m;
m.someFunction( ); // delete variable on stack!
Nothing in the original post says that the class *isn't* designed
to be only allocated using 'new' (the common approach with some
COM and CORBA objects, I'm told).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
On Jun 3, 12:01 am, Fred <fred.l.kleinsc hm...@boeing.co mwrote:
Is it legal to invoke my destructor from within an instance function?
Of course not. It's probably the most common case, in fact.
For example,
bool MyClass::someFu nction() {
SomeOtherClass: someFunction(th is);
// No other references to 'this'
// after this point
return true;
}
SomeOtherClass: :someFunction(M yClass m) {
The parameter should be a pointer, of course.
delete m;
}
I'm not too sure I like the added indirection. Most of the
time (something slightly over half), deletion is done by "delete
this".

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #5
On Jun 3, 3:40 am, Joshua <zhou.zhihua.m. ..@gmail.comwro te:
On Jun 3, 6:01 am, Fred <fred.l.kleinsc hm...@boeing.co mwrote:
Is it legal to invoke my destructor from within an instance
function?
For example,
bool MyClass::someFu nction() {
SomeOtherClass: someFunction(th is);
// No other references to 'this'
// after this point
return true;
}
SomeOtherClass: :someFunction(M yClass m) {
delete m;
}
Unsafe.
No more dangerous than anything else.
Myclass m;
m.someFunction( ); // delete variable on stack!
And how does the fact that someFunction() is a member change
anything. If the object has value semantics, no one should
delete it, since it shouldn't be allocated dynamically. And if
it has identity, all instances will be allocated dynamically,
and deletion will be in response to some external event. If the
event is processed by the object itself, then the object
deletes itself; if the event is processed by some other object,
then that object deletes the object. Obviously, there are other
kinds of objects as well, but these two represent well over 90%
of the objects in a typical simple application.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
On Jun 3, 3:01 am, Fred <fred.l.kleinsc hm...@boeing.co mwrote:
Is it legal to invoke my destructor from within an instance function?

For example,
bool MyClass::someFu nction() {
SomeOtherClass: someFunction(th is);
// No other references to 'this'
// after this point
return true;

}

SomeOtherClass: :someFunction(M yClass m) {
delete m;

}
It should be like:
SomeOtherClass: :someFunction(M yClass *m) {
delete m;
}

because you are passing a pointer.
>

Though its legal but its not safe.

Functions are made to call. Though there is no reference of 'this'
pointer after deleting it in some other class's member function, but
the caller who is calling the 'someFunction' of 'MyClass' (of course
through some MyClass variable) doesn't know about this deletion and
think what will happen if he try to access some other property of
MyClass?
Jun 27 '08 #7

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

Similar topics

52
27043
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".
11
4369
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
11
5313
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
4
3810
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For example ... C# Essentials (O'Reilly) - "Finalizers are class-only methods". Not sure what "class-only method" means in this context, but ususally I would assume that to mean it is static and would only be called once for the life of the...
4
4974
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int iSourceSampleRate, int iTargetSampleRate ) {
35
3329
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a member. However, I set a breakpoint at the destructor of this instance's class and it was never called!!! I can see how it might not get called at a deterministic time. But NEVER? So, I guess I need to know the rules about destructors. I would...
23
2611
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
1
2785
by: Suds | last post by:
Hi, I'm having an issue with invoking a Generic method that takes Generic Arguments. My method signature is public void GenericMethodWithGenericArguments<E, V>(List<EtheFirstList, List<VtheSecondList); I pass the name of the method, the arguments for the "GenericMethodWithGenericArguments" to another method, which is supposed to invoke this method using the Invoke method in the MethodInfo class. My process of invocation is as follows
3
1727
by: Thijs | last post by:
Hello everybody, I´ve got a problem that´s slightly complicated (at least to me it seems) so I will try to state it as clearly as possible. At a certain point in time, I start a second thread that runs one of my functions. The function in this second raises events. As you might know, the eventhandler for this event is executed within this second thread and not within the first. It´s not possible to update properties of a user control...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10230
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...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
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
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
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.