473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can an object know it is dead?

Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?

Thanks in advance for all responses.

Best,
Dave

Feb 23 '07 #1
12 1919
"be***********@ yahoo.com" <be***********@ yahoo.comwrote:
Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?
It cannot be done portably. However you might try putting in a "dead"
flag that is set to true in the destructor, then in all the
member-functions assert( dead == false ) might work.

An alternative that is standards conforming is to set-up a locking
mechanism. A simple map<void*, size_twill work. Whenever an object
needs another (or itself) to stay alive, it increments the value in the
map associated with that object. In the destructor, the object can check
to make sure that its reference is zero. Something like this:

map<void*, size_tlock;

class Object {
~Object() {
assert( lock[this] == 0 );
}

void func() {
++lock[this];
// do work
--lock[this];
}
};

void user( Object* o ) {
++lock[o];
// work with 'o'
--lock[o];
}

Of course it is wise to wrap the increment and decrement in an object
(RAII)

class Locker {
const void* obj;
Locker( const void* obj ): obj(obj) {
++lock[obj];
}
~Locker() {
--lock[obj];
}
};
Feb 23 '07 #2
On Feb 23, 10:54 am, "better_cs_...@ yahoo.com"
<better_cs_...@ yahoo.comwrote:
Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?
Ignoring the threads aspect of this, the simplest way is to add a
private bool member and an isValid() member function:

[untested code]

class yourClass {
private:
bool valid_;
protected:
bool isValid() { return valid_; }
public:
yourClass() : valid_(true) {}
~yourClass() { valid_ = false; }
// Rest of yourClass
};

Now have each member function check isValid() before proceeding. For
debugging purposes, you might just add an assert(isValid( )); to the
beginning of each member function.

Threads make this much more complicated of course - you would need
some sort of mutex or other device to ensure that the object wasn't
destructed while in the member function.

Best regards,

Tom

Feb 23 '07 #3
be***********@y ahoo.com wrote:
Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?
Can't be done.

Smart pointers.
Feb 23 '07 #4
In article <11************ **********@q2g2 000cwa.googlegr oups.com>,
"Thomas Tutone" <Th***********@ yahoo.comwrote:
On Feb 23, 10:54 am, "better_cs_...@ yahoo.com"
<better_cs_...@ yahoo.comwrote:
Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?

Ignoring the threads aspect of this, the simplest way is to add a
private bool member and an isValid() member function:

[untested code]

class yourClass {
private:
bool valid_;
protected:
bool isValid() { return valid_; }
public:
yourClass() : valid_(true) {}
~yourClass() { valid_ = false; }
// Rest of yourClass
};

Now have each member function check isValid() before proceeding. For
debugging purposes, you might just add an assert(isValid( )); to the
beginning of each member function.

Threads make this much more complicated of course - you would need
some sort of mutex or other device to ensure that the object wasn't
destructed while in the member function.

Best regards,

Tom
The above isn't "standards-compliant" as requested. If another object is
created in the memory space vacated by the dead object, valid_ may be
true even though the object is dead.
Feb 23 '07 #5
Daniel T. wrote:
In article <11************ **********@q2g2 000cwa.googlegr oups.com>,
"Thomas Tutone" <Th***********@ yahoo.comwrote:
>Ignoring the threads aspect of this, the simplest way is to add a
private bool member and an isValid() member function:

[untested code]

class yourClass {
private:
bool valid_;
protected:
bool isValid() { return valid_; }
public:
yourClass() : valid_(true) {}
~yourClass() { valid_ = false; }
// Rest of yourClass
};
>
The above isn't "standards-compliant" as requested. If another object is
created in the memory space vacated by the dead object, valid_ may be
true even though the object is dead.
And how is this different than your "solution"?
Feb 23 '07 #6
Daniel T. wrote:
"be***********@ yahoo.com" <be***********@ yahoo.comwrote:
>Hello all,

I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.

Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?

It cannot be done portably. However you might try putting in a "dead"
flag that is set to true in the destructor, then in all the
member-functions assert( dead == false ) might work.
No, it won't. It will randomly fail.
Feb 23 '07 #7
On Feb 23, 11:17 am, Noah Roberts <u...@example.n etwrote:
better_cs_...@y ahoo.com wrote:
Hello all,
I suspect that a threading issue is leading to an object being
destructed in one thread context while one of its member functions is
still running in another thread context. As a means of confirming
this, I would like to have the object check if it is alive in the
referenced member function and report to me if it is not. However, I
cannot think of a standards-compliant way for an object to know that
it has been destructed.
Can anybody confirm that this *cannot* be done? If it cannot, can
anybody suggest any alternatives?

Can't be done.
I doubt I'm telling Noah anything he does not already
know. But clueless newbies (me last week) might not
know why it can't be done.

It can't be done internally to the destroyed object.
If the destructor has been called then accessing the
data members of that object is an undefined operation.
Anything an object will "know" after it has been
destructed is not reliable. Indeed, even accessing
could, in principle, cause problems, even aside from
the contents not being defined. It could, for example,
produce some such thing as an access violation, and
so produce a program crash that is difficult to
understand. Or, it could result in the object being
quite happy to run along as though it were not
destructed, if another part of the code reset that
memory location. That might well produce very hard
to debug behaviour.

The only way to keep track of such things is to
put some kind of resource guard around the creation
and destruction of the object. That is, you have
to keep track externally to the object.
Smart pointers.
An example of doing it externally to the object.
Socks

Feb 23 '07 #8
In article <er**********@a ioe.org>, Noah Roberts <us**@example.n et>
wrote:
Daniel T. wrote:
In article <11************ **********@q2g2 000cwa.googlegr oups.com>,
"Thomas Tutone" <Th***********@ yahoo.comwrote:
Ignoring the threads aspect of this, the simplest way is to add a
private bool member and an isValid() member function:

[untested code]

class yourClass {
private:
bool valid_;
protected:
bool isValid() { return valid_; }
public:
yourClass() : valid_(true) {}
~yourClass() { valid_ = false; }
// Rest of yourClass
};

The above isn't "standards-compliant" as requested. If another object is
created in the memory space vacated by the dead object, valid_ may be
true even though the object is dead.

And how is this different than your "solution"?
With my solution, an assert will fire if some part of the code attempts
to destroy an object that is in use. I agree though that it allows one
to attempt to use an object that was destroyed and that needs to be
fixed. Here is a revised version:

map<void*, size_tlock;

class Object {
Object() {
assert( lock.find(this) == lock.end() );
lock[this] = 0;
}
~Object() {
assert( lock[this] == 0 );
lock.erase( this );
}

void func() {
assert( lock.find(this) != lock.end() );
++lock[this];
// do work
--lock[this];
}
};

void user( Object* o ) {
assert( lock.find(o) != lock.end() );
++lock[o];
// work with 'o'
--lock[o];
}

With the above, if you attempt to destroy an object that is still being
used by someone, an assert will fire and if you attempt to create an
object in a memory location that is in use, an assert will fire.

Of course it is wise to wrap the increment and decrement in an object
(RAII)

class Locker {
const void* obj;
Locker( const void* obj ): obj(obj) {
assert( lock.find(obj) != lock.end() );
++lock[obj];
}
~Locker() {
--lock[obj];
}
};
Feb 23 '07 #9
Daniel T. wrote:
In article <er**********@a ioe.org>, Noah Roberts <us**@example.n et>
wrote:
>Daniel T. wrote:
>>In article <11************ **********@q2g2 000cwa.googlegr oups.com>,
"Thomas Tutone" <Th***********@ yahoo.comwrote:
Ignoring the threads aspect of this, the simplest way is to add a
private bool member and an isValid() member function:

[untested code]

class yourClass {
private:
bool valid_;
protected:
bool isValid() { return valid_; }
public:
yourClass() : valid_(true) {}
~yourClass() { valid_ = false; }
// Rest of yourClass
};
The above isn't "standards-compliant" as requested. If another object is
created in the memory space vacated by the dead object, valid_ may be
true even though the object is dead.
And how is this different than your "solution"?

With my solution, an assert will fire if some part of the code attempts
to destroy an object that is in use. I agree though that it allows one
to attempt to use an object that was destroyed and that needs to be
fixed. Here is a revised version:

map<void*, size_tlock;

class Object {
Object() {
assert( lock.find(this) == lock.end() );
lock[this] = 0;
}
~Object() {
assert( lock[this] == 0 );
lock.erase( this );
}

void func() {
assert( lock.find(this) != lock.end() );
++lock[this];
// do work
--lock[this];
}
};

void user( Object* o ) {
assert( lock.find(o) != lock.end() );
++lock[o];
// work with 'o'
--lock[o];
}

With the above, if you attempt to destroy an object that is still being
used by someone, an assert will fire and if you attempt to create an
object in a memory location that is in use, an assert will fire.

Of course it is wise to wrap the increment and decrement in an object
(RAII)

class Locker {
const void* obj;
Locker( const void* obj ): obj(obj) {
assert( lock.find(obj) != lock.end() );
++lock[obj];
}
~Locker() {
--lock[obj];
}
};
Your fixed version is also not portable. Any and all uses of a deleted
object illicit undefined behavior. It also offers 0 protection in a
non-debug environment. Concurrent programs are affected by too many
variables for this to work even in cases when it might in a serial
program. What needs to be done here is to make sure the object is not
deleted before it is done being used. To do that, the easiest method is
some kind of smart pointer such as boost::shared_p tr (if such is thread
safe and I don't know). Even pooling could fall prey to the same
problem if a thread deletes the pool before those working with objects
in it are done.

So even by standard your solution doesn't work. When you add in the
OP's requirements it is even less suitable.
Feb 23 '07 #10

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

Similar topics

2
7998
by: meng | last post by:
is it possible to use a single connection object shared by several tasks where each task is handled by a thread? these tasks call stored procedures that return record sets, no editing, update or delete. my objective is that db connection is expensive and each user can only have 1 connection object. each user submits a request to the web...
4
1795
by: Rick | last post by:
Hello, Below I create a static instance of an object, works well. public static BasicCounter GlobalCounter = new BasicCounter("GlobalCounter"); private void Page_Load(object sender, System.EventArgs e) { GlobalCounter.Increment();
30
2449
by: marc | last post by:
>From the book I am using as a reference I understood that applet tag was deprecated, but googling for information, and testing myself, it seems IE does not work properly with this new object tag. So my effort reading about the object tag was for nothing? Is there no way I can make this work for IE and Mozilla? <HTML> <HEAD> </HEAD>...
5
2503
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
16
2882
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList...
35
2256
by: eyoung | last post by:
I call a function that takes the unit price and quantity ordered to create an amount...it looks something like this. function calculateCost() { quantity=document.RFO.quantity.value; unitPrice=document.RFO.unitPrice.value; total=0; if(isPositiveInteger(quantity)) {
5
1394
by: atomik.fungus | last post by:
Hi I've made a kamikaze function wrapper that executes the function(s) when its constructor is called and ends calling its own destructor. I thought originally of this as a good idea to avoid some typing but after watching things that I didnt thought would compile i am starting to doubt. The original idea for doing so was that once...
139
5841
by: Joe Mayo | last post by:
I think I become more and more alone... Everybody tells me that C++ is better, because once a project becomes very large, I should be happy that it has been written in C++ and not C. I'm the only guy thinking that C is a great programming language and that there is no need to program things object oriented. Many people says also that they...
275
12094
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7434
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...
0
7692
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. ...
0
7946
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...
1
7457
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...
0
6026
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...
1
5360
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...
0
5078
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...
0
3491
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...
1
1045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.