Skip to content

Instantly share code, notes, and snippets.

@sergeant-wizard
Created September 30, 2014 01:21
Show Gist options
  • Save sergeant-wizard/74a29573bf957c83126f to your computer and use it in GitHub Desktop.
Save sergeant-wizard/74a29573bf957c83126f to your computer and use it in GitHub Desktop.
risky callback
#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