audreyt / node-webworker-threads

Lightweight Web Worker API implementation with native threads
https://npmjs.org/package/webworker-threads
Other
2.3k stars 149 forks source link

Memory Leak #164

Closed 823639792 closed 7 years ago

823639792 commented 7 years ago

Using BSON will memory leak。 When construct:

BSON::BSON() : ObjectWrap()
{
    // Setup pre-allocated comparision objects
        _bsontypeString.Reset(Nan::New<String>("_bsontype").ToLocalChecked());
        _longLowString.Reset(Nan::New<String>("low_").ToLocalChecked());
        _longHighString.Reset(Nan::New<String>("high_").ToLocalChecked());
        _objectIDidString.Reset(Nan::New<String>("id").ToLocalChecked());
        _binaryPositionString.Reset(Nan::New<String>("position").ToLocalChecked());
        _binarySubTypeString.Reset(Nan::New<String>("sub_type").ToLocalChecked());
        _binaryBufferString.Reset(Nan::New<String>("buffer").ToLocalChecked());
        _doubleValueString.Reset(Nan::New<String>("value").ToLocalChecked());
        _symbolValueString.Reset(Nan::New<String>("value").ToLocalChecked());
        _dbRefRefString.Reset(Nan::New<String>("$ref").ToLocalChecked());
        _dbRefIdRefString.Reset(Nan::New<String>("$id").ToLocalChecked());
        _dbRefDbRefString.Reset(Nan::New<String>("$db").ToLocalChecked());
        _dbRefNamespaceString.Reset(Nan::New<String>("namespace").ToLocalChecked());
        _dbRefDbString.Reset(Nan::New<String>("db").ToLocalChecked());
        _dbRefOidString.Reset(Nan::New<String>("oid").ToLocalChecked());
        _codeCodeString.Reset(Nan::New<String>("code").ToLocalChecked());
        _codeScopeString.Reset(Nan::New<String>("scope").ToLocalChecked());
        _toBSONString.Reset(Nan::New<String>("toBSON").ToLocalChecked());
        longString.Reset(Nan::New<String>("Long").ToLocalChecked());
        objectIDString.Reset(Nan::New<String>("ObjectID").ToLocalChecked());
        binaryString.Reset(Nan::New<String>("Binary").ToLocalChecked());
        codeString.Reset(Nan::New<String>("Code").ToLocalChecked());
        dbrefString.Reset(Nan::New<String>("DBRef").ToLocalChecked());
        symbolString.Reset(Nan::New<String>("Symbol").ToLocalChecked());
        doubleString.Reset(Nan::New<String>("Double").ToLocalChecked());
        timestampString.Reset(Nan::New<String>("Timestamp").ToLocalChecked());
        minKeyString.Reset(Nan::New<String>("MinKey").ToLocalChecked());
        maxKeyString.Reset(Nan::New<String>("MaxKey").ToLocalChecked());
}

but

 ~BSON() { }

So I edit the destructor:

BSON::~BSON() {
    _bsontypeString.Reset();
    _longLowString.Reset();
    _longHighString.Reset();
    _objectIDidString.Reset();
    _binaryPositionString.Reset();
    _binarySubTypeString.Reset();
    _binaryBufferString.Reset();
    _doubleValueString.Reset();
    _symbolValueString.Reset();
    _dbRefRefString.Reset();
    _dbRefIdRefString.Reset();
    _dbRefDbRefString.Reset();
    _dbRefNamespaceString.Reset();
    _dbRefDbString.Reset();
    _dbRefOidString.Reset();
    _codeCodeString.Reset();
    _codeScopeString.Reset();
    _toBSONString.Reset();

    longString.Reset();
    objectIDString.Reset();
    binaryString.Reset();
    codeString.Reset();
    dbrefString.Reset();
    symbolString.Reset();
    doubleString.Reset();
    timestampString.Reset();
    minKeyString.Reset();
    maxKeyString.Reset();
}

The program works well, but I think BSON should only construct once, and used any where. not new and delete anywhere.

@audreyt

audreyt commented 7 years ago

Thanks for the contribution! A separate pull request for making BSON Singleton is welcome too.