0

If i have a pointer (static address) that will always point to a specific variable address (ex.health) then how to check if the game.exe destroyed and deallocates this object before program reads and writes to this non existing object at this time?

static DWORD baseAddress = 0x00000 ;
static DWORD healthPtr = (DWORD)(baseAddress + health_offset);

//when the game deallocates and destroys health object it's value become (??) garbage causing crash on reading or writing to it!

//So how to check here if the address pointing to health is realy pointing to a valid health object not a destroyed non sense garbage?

*(int*)(healthPtr) = 100;
// writing to a deallocated object will cause a crash
  • 3
    Use `shared_ptr` and `weak_ptr` if it's possible for the pointer to be invalidated. There's no way to check this with just raw pointers. – Stephen Newell Oct 01 '21 at 00:11
  • 4
    Pointers are blissfully ignorant of anything other than the address they hold and there is no back channel from object to pointer (think of the book-keeping that would require) so there is no way to inform a pointer that the object it references is no more. – user4581301 Oct 01 '21 at 00:23
  • The nullptr check before reading or writing to health var realy worked for my situation!! if((int*)healthptr != nullptr) so if true then the health var is holding int value so i can read and write it and if false so var contains garbage so i can stop reading and writing it – john.shika Oct 11 '21 at 01:28

0 Answers0