forgottenserver icon indicating copy to clipboard operation
forgottenserver copied to clipboard

fix crash and leak in action:register and moveevent:register

Open yamaken93 opened this issue 4 years ago • 1 comments

Pull Request Prelude

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

yamaken93 avatar Sep 27 '21 00:09 yamaken93

~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.

MillhioreBT avatar Jun 28 '22 00:06 MillhioreBT

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?

Codinablack avatar Apr 17 '24 04:04 Codinablack