简体   繁体   中英

shared_ptr, vector, emplace_back and usage pointer outside

I'm working on an application that will generate some code and speed is not an issue but I don't want memory leaks.

I'm a bit confused with shared_ptr coming from other languages with garbage collectors.

If I understand correctly basically if I use shared_ptr on every pointer it will behave in a similar manner as a garbage collector as it will remvove the object when it is no longer referenced.

I am confused what happens when I use emplace_back in a vector of shared_ptr. Lets say I have this function:

void fun(MyObject* o){
    std::vector<std::shared_ptr<MyObject>> v;
    v.emplace_back(o);
}

Now I create a pointer and call the function:

MyObject* o = new MyObject();
fun(o);
//Is o still here?

What happens with o? emplace_back somehow creates a shared_ptr out of my naked pointer and when the vector goes out of scope will it remove o?

I guess the proper way is to shared_ptr everything if I dont care about perfomance but code will look so bad...

EDIT: I understand it works as expected. For my application I think I will go with good old new and delete. The application is a tree with different nodes so calling delete at the end should call each children delete safely from top to down.

What happens with o?

It becomes a dangling pointer, because the destructor of shared_ptr deleted the object it is pointing to. But that's only because your vector was destroyed (because it went out of scope). If your vector wasn't destroyed, then o would still be pointing to the allocated object, which would now be managed by the shared pointer in your vector.

I guess the proper way is to shared_ptr everything if I dont care about perfomance but code will look so bad...

No, the proper way is to learn how to use C++ properly, and not just try to jam in the idioms you learned from other languages. Don't worry, you can still write inefficient code, if you so desire.

What happens with o? emplace_back somehow creates a shared_ptr out of my naked pointer and when the vector goes out of scope will it remove o?

This is correct. You transfer the ownership of the memory to the shared_ptr which itself is owned by the vector. When the vector gets destroyed, the object o points to will get destroyed by the shared_ptr's destructor as well. Generally, your o-object will get destroyed in the shared_ptr's destructor if there exists no other shared_ptr holding a reference to it.

I guess the proper way is to shared_ptr everything if I dont care about perfomance but code will look so bad...

No. The proper way is to use a smart pointer for the purpose it is designed for. shared_ptr is for sharing the ownership of a resource: you want to make sure that a resource stays alive as long as any instance in your program (eg a thread) holds a reference to it. There are other smart pointers as well which may rather suit your needs, especially unique_ptr. Also, you should ask yourself if you really need dynamic memory allocations to begin with. If possible, stack-based variables should be preferred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM