Suppose I have a function that takes a pointer as a parameter. This function may throw an exception because it uses std::vector<>::push_back() to control the life cycle of this pointer. If I declare it like this:
void manage(T *ptr);
and name it as follows:
manage(new T());
if it throws an exception by clicking on the pointer to std::vector<> , I really got a memory leak, right?
Declares a function as follows:
void manage(std::auto_ptr<T> ptr);
solve my problem?
I would expect it to first allocate std::auto_ptr on the stack (something that I suppose could never throw an exception) and let it acquire ownership of the pointer. Safe
Then inside the function I would press the raw pointer to std::vector<> , which is also safe: if this fails, the pointer will not be added, but the smart pointer will still own the pointer so that it is destroyed. If the click succeeds, I will remove the ownership of the smart pointer above that pointer and return it: this cannot throw an exception, so it will always be ok.
Are my theories correct?
- Change -
No, I think I can’t do this. To do this, you need to take a non-constant link to rvalue (to take property from the smart pointer). I would have to write
std::auto_ptr<T> ptr(new T()); manage(ptr);
for this, something that is completely uncomfortable in my case. I am writing this so that I can implement RAII without polluting the code much. Then it would not help. This will be trick 22.
- Change 2 -
Pulling out what Jason Orendorf said, here, for quick reference to readers, the final solution is as follows:
void manage(T *ptr) { std::auto_ptr<T> autoPtr(ptr); vector.push_back(ptr); autoPtr.release(); }
This solves the problem of a useless non-constant rvalue reference.
When I finish this class, I will code, I will post it here if anyone finds it useful.
- Change 3 -
Well, there has been a lot of discussion, and there are key points that I should have clarified before. In general, when I post to stackoverflow, I try to explain the reason for my questions and, in general, is completely useless. So this time I thought that I should go straight to the point. Turns out it doesn't work very well XD
Unfortunately, my brain is now stalled, so I think that I can’t even explain correctly what I first decided to do for my purposes. I am trying to find a good solution for atomic operations and a code-safe code entry that is suitable for many cases, but in fact, I cannot handle it. XD I think this is something that I will only learn with time.
I am truly a new C ++ programmer and I am focused on game development. When an exception is thrown into the game engine, this is the end of the execution. The system will free up all the memory for my process, so it doesn’t matter if one or two pointers flow here and there. Now, when I develop a server application, it is difficult for me to deal with exceptions, because an exception cannot lead to a server crash; he should "collapse the request."
That is: "Well, the client, unfortunately, the developers did not foresee this condition, so you will have to try it later (until now it is basically the same as with the game engine, nothing happens but is not isolated from the context only request, not the whole process.) But do not panic, because everything is left in the correct state (here is one of the differences. so the operating system cannot free resources for you, in addition, you should pay attention to canceling operations so far, so you don’t completely block the user account broadcaster, for example, or even a full server service). ".
I will just write more and write down my problems so that next time I can write a better question. I was not ready to ask it now, I'm sorry.
Thanks so much for your answers, I really like stackoverflow. It is absolutely amazing how quickly my questions are answered and how your answers are explained. Thanks.