Skip to content

Commit

Permalink
call ncnn::destroy_gpu_instance() explicitly
Browse files Browse the repository at this point in the history
fix #6
  • Loading branch information
nlzy committed Jun 8, 2021
1 parent a18b52e commit dda5dca
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/vsw2xnvk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
#include "waifu2x.hpp"
#include "VSHelper.h"

static ncnn::Mutex instanceLock;
static int instanceCounter = 0;

static int tryCreateGpuInstance() {
ncnn::MutexLockGuard lg(instanceLock);
if (instanceCounter++ == 0) {
return ncnn::create_gpu_instance();
} else {
return 0;
}
}

static void tryDestoryGpuInstance() {
ncnn::MutexLockGuard lg(instanceLock);
if (--instanceCounter == 0) {
ncnn::destroy_gpu_instance();
}
}

typedef struct {
VSNodeRef *node;
VSVideoInfo vi;
Expand Down Expand Up @@ -89,6 +108,7 @@ static void VS_CC filterFree(void *instanceData, VSCore *core, const VSAPI *vsap
vsapi->freeNode(d->node);
delete d->waifu2x;
delete d;
tryDestoryGpuInstance();
}

static void VS_CC filterCreate(const VSMap *in, VSMap *out, void *userData, VSCore *core, const VSAPI *vsapi) {
Expand All @@ -102,7 +122,7 @@ static void VS_CC filterCreate(const VSMap *in, VSMap *out, void *userData, VSCo
do {
int err;

err = ncnn::create_gpu_instance();
err = tryCreateGpuInstance();
if (err) {
err_prompt = "create gpu instance failed";
break;
Expand Down Expand Up @@ -214,6 +234,7 @@ static void VS_CC filterCreate(const VSMap *in, VSMap *out, void *userData, VSCo
if (err_prompt) {
vsapi->setError(out, (std::string{"Waifu2x-NCNN-Vulkan: "} + err_prompt).c_str());
vsapi->freeNode(d.node);
tryDestoryGpuInstance();
return;
}

Expand Down

0 comments on commit dda5dca

Please sign in to comment.