Thread: Vapoursynth
View Single Post
Old 10th February 2021, 10:34   #4284  |  Link
feisty2
I'm Siri
 
feisty2's Avatar
 
Join Date: Oct 2012
Location: void
Posts: 2,633
@Myrsloik
should I release *frameData instantly if an exception is thrown from the arAllFramesReady branch? or should I just leave it unhandled and expect it gets deleted when getFrame() is invoked again with arError?

basically, this
Code:
try {
    if (auto& ResourceHandle = reinterpret_cast<ResourceType*&>(*frameData); activationReason == arInitial)
        ResourceHandle = new auto{ FilterInstance->AcquireResources(...) };
    else if (activationReason == arAllFramesReady) {
        auto ManagedResourceHandle = std::unique_ptr<ResourceType>{ ResourceHandle };
        auto GeneratedFrame = FilterInstance->DrawFrame(*ManagedResourceHandle, ...);
        // if DrawFrame() throws an exception, *ResourceHandle is instantly released
        // by the destructor of std::unique_ptr

        return GeneratedFrame.ReleaseOwnership();
    }
    else if (activationReason == arError)
        delete ResourceHandle;
        // possible double-free here?

    return nullptr;
}
catch (RuntimeError& ErrorMessage) {
    FrameContext.RaiseError(ErrorMessage);
    return nullptr;
}
or this
Code:
try {
    if (auto& ResourceHandle = reinterpret_cast<ResourceType*&>(*frameData); activationReason == arInitial)
        ResourceHandle = new auto{ FilterInstance->AcquireResources(...) };
    else if (activationReason == arAllFramesReady) {
        auto GeneratedFrame = FilterInstance->DrawFrame(*ResourceHandle, ...);
        // no leak if DrawFrame() throws an exception
        // *ResourceHandle will be released later from the arError branch

        delete ResourceHandle;
        return GeneratedFrame.ReleaseOwnership();
    }
    else if (activationReason == arError)
        delete ResourceHandle;
    return nullptr;
}
catch (RuntimeError& ErrorMessage) {
    FrameContext.RaiseError(ErrorMessage);
    return nullptr;
}
?
feisty2 is offline   Reply With Quote