Created
September 30, 2014 01:21
-
-
Save sergeant-wizard/74a29573bf957c83126f to your computer and use it in GitHub Desktop.
risky callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <unistd.h> | |
class bar{ | |
public: | |
bar(){} | |
~bar(){} | |
void setPointer(void* pointer){ | |
std::cout << "setPointer called" << std::endl; | |
_pointer = pointer; | |
} | |
void onFinish(){ | |
if(_pointer) { | |
std::cout << "pointer is alive" << std::endl; | |
}else | |
std::cout << "pointer is dead" << std::endl; | |
} | |
private: | |
void* _pointer; | |
}; | |
class foo{ | |
public: | |
foo(){} | |
~foo(){} | |
void setBadPointer(bar& bar){ | |
bar.setPointer(this); | |
} | |
}; | |
int main(){ | |
bar _bar; | |
foo* _foo = new foo; | |
_foo->setBadPointer(_bar); | |
delete _foo; | |
_foo = nullptr; | |
_bar.onFinish(); | |
// prints "pointer is alive" | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment