From 35fce61f1fd2c3300bb22fb5b8afb572500ac2a5 Mon Sep 17 00:00:00 2001 From: Zhang Qiang Date: Tue, 6 Sep 2016 00:04:31 +0800 Subject: [PATCH] fix get_result(#11) add pointer check before doing segment(close #12) --- src/nodescws.cc | 50 ++++++++++++++++++++++++++++++++----------------- test/new_api.js | 15 ++++++++++++++- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/nodescws.cc b/src/nodescws.cc index c0b2815..b00528b 100644 --- a/src/nodescws.cc +++ b/src/nodescws.cc @@ -196,8 +196,8 @@ void NodeScws::ScwsInit() void NodeScws::ScwsSegment(const Nan::FunctionCallbackInfo& info) { if (!info[0]->IsString()) { - Nan::ThrowTypeError("[scws ERROR] Argument \ - should be the string to segment"); + Nan::ThrowTypeError("[scws ERROR] Argument " + "should be the string to segment"); // return undefined info.GetReturnValue() .Set(Nan::Undefined()); @@ -206,35 +206,48 @@ void NodeScws::ScwsSegment(const Nan::FunctionCallbackInfo& info) NodeScws *nscwsp = ObjectWrap::Unwrap(info.Holder()); scws_t scws = nscwsp->scws; + // destroyed? + if (!scws) { + Nan::ThrowError("[scws ERROR] scws instance not found, " + "possibly already been destroy()ed."); + // return undefined + info.GetReturnValue() + .Set(Nan::Undefined()); + return; + } + // convert v8::String to char * std::string Text(*Nan::Utf8String(info[0]->ToString())); char *text = (char *)Text.c_str(); scws_send_text(scws, text, strlen(text)); - scws_res_t res, res0; + scws_res_t res; int res_wc = 0; long memsize = RESMEMSTEP * sizeof(scws_result); int memsteps = 1; nscwsp->result_raw_ = (scws_result *)malloc(memsize); - res = res0 = scws_get_result(scws); + res = scws_get_result(scws); while (res != NULL) { - memcpy(&nscwsp->result_raw_[res_wc], res, sizeof(*res)); - res_wc++; - if (res_wc >= RESMEMSTEP * memsteps) { - long new_size = RESMEMSTEP * (memsteps + 1) * sizeof(scws_result); - if ((nscwsp->result_raw_ = (scws_result *)realloc(nscwsp->result_raw_, new_size)) == NULL) { - Log(NODESCWS_MSG_ERR, "Failed to allocate memory for results\n"); - free(nscwsp->result_raw_); - scws_free(scws); - info.GetReturnValue().Set(Array::New(0)); + while (res != NULL) { + memcpy(&nscwsp->result_raw_[res_wc], res, sizeof(*res)); + res_wc++; + if (res_wc >= RESMEMSTEP * memsteps) { + long new_size = RESMEMSTEP * (memsteps + 1) * sizeof(scws_result); + if ((nscwsp->result_raw_ = (scws_result *)realloc(nscwsp->result_raw_, new_size)) == NULL) { + Log(NODESCWS_MSG_ERR, "Failed to allocate memory for results\n"); + free(nscwsp->result_raw_); + scws_free(scws); + info.GetReturnValue().Set(Array::New(0)); + } + memsteps++; } - memsteps++; + res = res->next; } - res = res->next; + scws_free_result(res); + res = scws_get_result(scws); } - scws_free_result(res0); Local resArr = Nan::New(res_wc); for (int i = 0; i < res_wc; i++) { @@ -259,9 +272,12 @@ void NodeScws::ScwsSegment(const Nan::FunctionCallbackInfo& info) void NodeScws::ScwsDestroy(const Nan::FunctionCallbackInfo& info) { - info.GetReturnValue().Set(Nan::New("destroy").ToLocalChecked()); NodeScws* nscwsp = ObjectWrap::Unwrap(info.Holder()); + + // only free scws, other Local values will be GCed by v8 scws_free(nscwsp->scws); + nscwsp->scws = NULL; + info.GetReturnValue().Set(Nan::True()); } diff --git a/test/new_api.js b/test/new_api.js index 500456c..370e60b 100644 --- a/test/new_api.js +++ b/test/new_api.js @@ -22,6 +22,17 @@ catch (e) { console.log(); } +try { + var scws = new NodeScws({}); + scws.destroy(); + scws.segment("abc"); +} +catch (e) { + console.log("should raise error: already destroy()ed"); + console.log(e); + console.log(); +} + const conf = { charset: "utf8", debug: false, @@ -32,11 +43,13 @@ const conf = { ignorePunct: true, multi: "duality" } -var content = fs.readFileSync(__dirname + "/test_doc.txt", {encoding: "utf8"}); +var content = fs.readFileSync(__dirname + "/test_doc.txt", {encoding: "utf8"}), + content_en = "引言 VMwareTools 是 VMware 虚拟机中很重要的一个工具包,有些时候在虚拟机中安装完操作系统会缺少网卡驱动,不能上网,这时只要安装 VMwareTools 就可以解决问题,下面以 CentOS 为例,来说明 VMwareTools 的安装方法"; var start = new Date().valueOf(), end; var scws = new NodeScws(conf); // console.log(scws.getConfig()); console.log(scws.segment(content)); +console.log(scws.segment(content_en)); console.log(scws.destroy()); end = new Date().valueOf(); console.log("time used: " + (end - start) + "ms");