Created
March 14, 2016 02:03
-
-
Save if1live/be4a84b4362103a45159 to your computer and use it in GitHub Desktop.
use after delete
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 <string> | |
#include <cstring> | |
class BrokenMemory { | |
public: | |
static BrokenMemory *Get(); | |
public: | |
BrokenMemory() : data_("EPIC FAIL") {} | |
const char *data() const { return data_.data(); } | |
private: | |
static BrokenMemory *obj; | |
std::string data_; | |
}; | |
BrokenMemory *BrokenMemory::obj = NULL; | |
BrokenMemory *BrokenMemory::Get() { | |
if(obj != NULL) { | |
delete(obj); | |
obj = NULL; | |
} | |
if(obj == NULL) { | |
obj = new BrokenMemory(); | |
} | |
return obj; | |
} | |
int main() | |
{ | |
BrokenMemory *before = BrokenMemory::Get(); | |
BrokenMemory *after = BrokenMemory::Get(); | |
printf("before\t:%s\n", before->data()); | |
printf("after\t:%s\n", after->data()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment