Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fix memory leak by properly closing charsetDetector
  • Loading branch information
mhertsch authored and sonicdoe committed Mar 10, 2017
1 parent 2e3aa33 commit d443569
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions icuWrapper.cpp
Expand Up @@ -28,27 +28,31 @@ NAN_METHOD(DetectCharacterEncoding) {

if(U_FAILURE(errorCode)) {
Nan::ThrowError("Failed to set ICU charset detector’s text.");
ucsdet_close(charsetDetector);
return;
}

charsetMatch = ucsdet_detect(charsetDetector, &errorCode);

if(U_FAILURE(errorCode)) {
Nan::ThrowError("Failed to detect charset.");
ucsdet_close(charsetDetector);
return;
}

const char *charsetName = ucsdet_getName(charsetMatch, &errorCode);

if(U_FAILURE(errorCode)) {
Nan::ThrowError("Failed to get name from charset match.");
ucsdet_close(charsetDetector);
return;
}

int32_t confidence = ucsdet_getConfidence(charsetMatch, &errorCode);

if(U_FAILURE(errorCode)) {
Nan::ThrowError("Failed to get confidence from charset match.");
ucsdet_close(charsetDetector);
return;
}

Expand All @@ -57,6 +61,7 @@ NAN_METHOD(DetectCharacterEncoding) {
obj->Set(Nan::New<v8::String>("confidence").ToLocalChecked(), Nan::New<v8::Number>(confidence));

info.GetReturnValue().Set(obj);
ucsdet_close(charsetDetector);
}

void Init(v8::Local<v8::Object> exports) {
Expand Down

0 comments on commit d443569

Please sign in to comment.