0

I asked a question:

Detecting if an object is still active or it has been destroyed

Considering that I cannot use libraries, there are no good out of the box solutions in C++. So, is it a bad practice to check if the object has been destroyed by analyzing memory where the deleted object resided, consider the code below:

#include <stdio.h>

class Foo {
 public:
    static unsigned int foo_id_gen;    // unique id generator
    unsigned int id;
    int a;
    Foo() {
      ++foo_id_gen;
      if (foo_id_gen == 0) {           // Handle rollover of unique ID
        ++foo_id_gen;
      }
      id = foo_id_gen;                 // Set unique ID
    }
    ~Foo() {
      id = 0;                          // Set unique ID to Zero
    }
};

unsigned int Foo::foo_id_gen = 0;

class FooPtr {
 public:
    Foo* ptr;
    unsigned int id;
    explicit FooPtr(Foo* foo_obj_ptr) {
      ptr = foo_obj_ptr;
      id = foo_obj_ptr->id;
    }
    bool valid() {
      return (ptr->id == id);
    }
};

int main() {
  Foo* A = new Foo;
  FooPtr APtrOjb(A);
  printf("Is valid?: %s\n", APtrOjb.valid() ? "Valid" : "Not Valid");
  delete A;
  printf("Is valid?: %s\n", APtrOjb.valid() ? "Valid" : "Not Valid");
}

When the object of class Foo is created, it gets a unique ID that can never be zero, when the object is deleted the destructor sets the unique ID to zero. Then the object of type FooPtr can check if the object of class Foo is still active by checking if the id's match.

Note: I realize that checking id does not 100% grantee the result since there is a small chance that a match can happen when the memory is alocated for other purposes. So lets assume that the id verification method can be strengthened to grantee 100% result, is this a viable solution?

Someone suggested that this question is similar to C++ delete - It deletes my objects but I can still access the data?, but his question is about deleting data, but my question is about detecting if object has been deleted.

Community
  • 1
  • 1
user1135541
  • 1,781
  • 3
  • 19
  • 41
  • possible duplicate of [C++ delete - It deletes my objects but I can still access the data?](http://stackoverflow.com/questions/1930459/c-delete-it-deletes-my-objects-but-i-can-still-access-the-data) – Bernhard Barker Jun 22 '15 at 16:02
  • 1
    This is no solution. You are trying to reinvent `shared_ptr`/`weak_ptr`. when used with `make_shared` the memory footprint is very similar to your class but without polluting your class. Also it is proven to work and it is legal code. Why can't you use `std` or `boost` implementation of `shared_ptr`? – stefan Jun 22 '15 at 16:46
  • @stefan, thank you for steering me away from bad design... – user1135541 Jun 22 '15 at 17:17

1 Answers1

2

It is very bad, accessing deleted objects as if they were not deleted will in the general case crash. There is no guarantee that the memory is still mapped inside the process and it could result in a virtual memory page fault.

It is also likely that the memory will be re-used for a different instance of the same class, so it will appear to be valid, but it is not.

Generally, this design will lead to bugs that are very hard to analyse.

Your test program is also very limited in scope. It cannot say anything about the behaviour of larger programs.

Mats
  • 8,528
  • 1
  • 29
  • 35
  • That is why the ID changes for every instance, so paragraph 2 is not NA, but paragraph 1 is something to think about, but I do not have an OS to deal with (no page faults), so this is strictly C++ issue. – user1135541 Jun 22 '15 at 15:42
  • Unless you are actually writing the OS yourself (in which case you can do whatever you want, as you're responsible for all of the memory on the system), then you do have an OS to deal with, even if it is a simple OS. – Donnie Jun 22 '15 at 17:25