dimanche 23 décembre 2018

what happend when delete the raw pointer from shared_ptr get()

I wrote some code like this:

shared_ptr<int> r = make_shared<int>();
int *ar = r.get();

delete ar; // report double free or corruption
// still some code

when the code run to "delete ar;", the program crushed, and report "double free or corruption", I'm coffused why double free? the "r" still in the scope, and not poped-off from stack. Do the delete operator do something magic?? it does know the raw pointer is handling by a smart pointer current now? and then counter in "r" be decremented to zero automactically? I know the operations is not recommended, but I want to know why?

Aucun commentaire:

Enregistrer un commentaire