From a9eed184c292e06131761d5a54fc7c01001b8ea4 Mon Sep 17 00:00:00 2001 From: arch1t3cht Date: Sun, 15 Oct 2023 22:58:23 +0200 Subject: [PATCH 1/2] Revert "avisynth: Only increase refcount when fully initialized" This reverts commit f5a730fa45343faa157af1c988c98e5badff7a6d. --- src/avisynth_wrap.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/avisynth_wrap.cpp b/src/avisynth_wrap.cpp index 1dc5d71b2f..eb957fa3cd 100644 --- a/src/avisynth_wrap.cpp +++ b/src/avisynth_wrap.cpp @@ -67,7 +67,7 @@ const AVS_Linkage *AVS_linkage = nullptr; typedef IScriptEnvironment* __stdcall FUNC(int); AviSynthWrapper::AviSynthWrapper() { - if (!avs_refcount){ + if (!avs_refcount++) { #ifdef _WIN32 #define CONCATENATE(x, y) x ## y #define _Lstr(x) CONCATENATE(L, x) @@ -94,8 +94,6 @@ AviSynthWrapper::AviSynthWrapper() { if (!env) throw AvisynthError("Failed to create a new avisynth script environment. Avisynth is too old?"); - avs_refcount++; - AVS_linkage = env->GetAVSLinkage(); // Set memory limit From 858f4acf3544e7938ac3c6878fb098b3b4fc386f Mon Sep 17 00:00:00 2001 From: arch1t3cht Date: Sun, 15 Oct 2023 23:08:39 +0200 Subject: [PATCH 2/2] avisynth: Decrease refcount again when constructor fails Proper fix to the issue that f5a730fa45343faa157af1c988c98e5badff7a6d was trying to fix. Fixes arch1t3cht/Aegisub#61 . --- src/avisynth_wrap.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/avisynth_wrap.cpp b/src/avisynth_wrap.cpp index eb957fa3cd..3473efd941 100644 --- a/src/avisynth_wrap.cpp +++ b/src/avisynth_wrap.cpp @@ -53,6 +53,7 @@ // Allocate storage for and initialise static members namespace { int avs_refcount = 0; + bool failed = false; #ifdef _WIN32 HINSTANCE hLib = nullptr; #else @@ -66,7 +67,7 @@ const AVS_Linkage *AVS_linkage = nullptr; typedef IScriptEnvironment* __stdcall FUNC(int); -AviSynthWrapper::AviSynthWrapper() { +AviSynthWrapper::AviSynthWrapper() try { if (!avs_refcount++) { #ifdef _WIN32 #define CONCATENATE(x, y) x ## y @@ -101,6 +102,9 @@ AviSynthWrapper::AviSynthWrapper() { if (memoryMax) env->SetMemoryMax(memoryMax); } +} catch (AvisynthError const&) { + avs_refcount--; + throw; } AviSynthWrapper::~AviSynthWrapper() {