fix crash and leak in action:register and moveevent:register
Pull Request Prelude
- [x] I have followed proper The Forgotten Server code styling.
- [x] I have read and understood the contribution guidelines before making this PR.
- [x] I am aware that this PR may be closed if the above-mentioned criteria are not fulfilled.
Changes Proposed
Better registerLuaEvent code, allow to register the same action/moveevent to aid, uid, item id and position. No use after delete anymore in action:register and moveevent:register. No memory leak/waste of memory since we borrow/steal the vector from the original event making the future copies copy empty vector.
Issues addressed: #3692
~I think the compiler knows if this should be returned with or without movement samantic, but since I can't confirm it, I think it would be best to do that @ranisalt suggested.~
Definitely, Ranisalt's suggestion is the correct option for this case. Although moving the vectors means they no longer belong to the object, the object will maintain valid but undefined vectors. I don't think anyone would want to use these undefined vectors, but it's better to maintain correct logic throughout the code.
I'm unsure about this. If I recall correctly, using a variable after moving it is undefined behavior.
It would probably be better to create a new one on the stack, swap with the one on the current object, and return the one on the stack (it will elide the copy/move):
std::vector newVector{}; std::swap([this->]oldVector, newVector); return newVector;
So basically something like this is needed to fix this PR
std::vector<uint32_t> borrowItemIdRange() {
std::vector<uint32_t> temp;
temp.swap(itemIdRange);
return temp;
}
Right? If all the new "borrowed" methods looked like this, or along these lines (obviously variable names would change), then the PR would be solid?