2

Let's say I have a class:

class Foo {
 public:
    int a;
}

This class is instantiated via new operator and destroyed via delete operator by object B of class Bar.

If object B of class Bar, instantiates new object A of class Foo and then it passes pointer of object A to object of C of class Baz. If Object B of class Bar has been self deleted, then how can object C of class Baz detect if object A is active or it has been deleted. Note: this is on a very small embedded system, therefore no libraries can be used, not even std. See the sequence diagram below.

|------|                               |------|
| B:Bar|                               | C:Buz|
|------|                               |------|
   ||          |-------|                  ||
   ||---New--->| A:Foo |                  ||
   ||          |-------|                  ||
   ||             ||                      ||
   ||             ||                      ||
   ||-----C:Buz.set_foo_Ptr(&A:Foo);----->||
   ||             ||                      ||
   ||---Delete--->X                       ||
   ||                                     ||
   X  (B:Bar Self Deleted)                ||
                                          ||
                          (Can C:Buz know if A:Foo
                          has been deleted or not ?)
user1135541
  • 1,781
  • 3
  • 19
  • 41
  • 3
    Seems like a good candidate for [`std::weak_ptr`](http://en.cppreference.com/w/cpp/memory/weak_ptr) – Cory Kramer Jun 22 '15 at 14:19
  • I don't know that the standard says about this usage, but would a dynamic cast work (and reliably)? – donjuedo Jun 22 '15 at 14:21
  • Do you need it to *not* be deleted? Or is deleting potentially OK? – Barry Jun 22 '15 at 14:22
  • 1
    Your problem is that you're creating and handing out pointers but there's no single entity responsible for destroying them. This is why we have std::shared_ptr and std::weak_ptr. – Robinson Jun 22 '15 at 14:23
  • this Q is little bit similar to http://stackoverflow.com/questions/677653/does-delete-call-the-destructor..., hope so this would help u –  Jun 22 '15 at 14:40
  • Forget the ASCII art. Can we see the classes? The important thing is, how many pointers refer to the potentially-dead object at once? If there may be several, what is the relationship between them? – Potatoswatter Jun 22 '15 at 14:48
  • @Potatoswatter I am working on system architecture and trying evaluate robustness of an approach, thanks... – user1135541 Jun 22 '15 at 15:28
  • Most probably you are right in the middle of bad design and you are asking us to help you shoot yourself in the foot. In that order: try to fix your design; use smart pointers (that's what you are trying to reinvent); B shall reset the pointer at C before performing delete. To your statement _very small embedded system, therefore no libraries can be used, not even std_ - bull****. If your compiler does support c++11 or boost use the libs. – stefan Jun 22 '15 at 16:12
  • @stefan, no one is getting shot in the foot here, with so many of you cringing, there is no way I would use this approach. – user1135541 Jun 22 '15 at 17:08

4 Answers4

4

You have ownership problems here. Typically you would use one of the stl smart pointers to handle this, but as you said no stl... you're going to have to manage ownership of the pointer very carefully and explicitly.

Passing a pointer to A around without its enclosing object is a bad idea. Now you C owns A but B also owns A. This is not good. Either pass B to C and let C access A via B, or provide accessor functions. Or pass A to C and ensure that C is the only object that manages A (ie C will delete all A objects regardless).

I guess if you must, you coudl include a flag with B that determines who has ownership of A - by default it is set to 'B' but once passed across it can be set to 'C', and is used when B is deleted to tell B whether to delete A or not.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
2

Well, by definition you can't, since an object that has been destroyed isn't there anymore. And you can't check that which doesn't exist...

A more practical approach would be the weak_ptr that has been pointed out already, or a signal&slot combo to notify interested objects of the destruction of A.

JvO
  • 3,036
  • 2
  • 17
  • 32
  • Thanks, that is what I thought, but wanted to get more input. I am asking a follow up question: http://stackoverflow.com/questions/677653/does-delete-call-the-destructor. – user1135541 Jun 22 '15 at 15:32
  • So after asking second question, @gbjbaanb is steering me in a better direction, so I am going to mark him as answer, give you an up vote, thanks... – user1135541 Jun 22 '15 at 17:06
  • No problem; sometimes a better solution for your problems arrives. Stackoverflow is not an oracle, although it comes very, very close :) – JvO Jun 23 '15 at 03:24
1

You can use B's destructor to call C:Buz.set_foo_Ptr(nullptr); and then C can test for the nullptr before using it.

Dalzhim
  • 1,970
  • 1
  • 17
  • 34
0

Two options:

  • If you just need to know, use a std::atomic<bool> variable which says if the object still exists
  • Instead, if onject C still needs to access the object, use smart pointers.
Claudio
  • 10,614
  • 4
  • 31
  • 71