Skip to content

Commit 41e6daa

Browse files
committed
Expose deallocation functions in NodeJS binding
1 parent f397006 commit 41e6daa

File tree

4 files changed

+10
-18
lines changed

4 files changed

+10
-18
lines changed

native_client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ After following the above build and installation instructions, the Node.JS bindi
100100

101101
```
102102
cd native_client/javascript
103-
make package
103+
make build
104104
make npm-pack
105105
```
106106

native_client/javascript/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env node
2-
32
'use strict';
43

54
const Fs = require('fs');
@@ -69,6 +68,7 @@ function metadataToString(metadata) {
6968
for (var i = 0; i < metadata.num_items; ++i) {
7069
retval += metadata.items[i].character;
7170
}
71+
Ds.FreeMetadata(metadata);
7272
return retval;
7373
}
7474

@@ -135,5 +135,6 @@ audioStream.on('finish', () => {
135135
}
136136
const inference_stop = process.hrtime(inference_start);
137137
console.error('Inference took %ds for %ds audio file.', totalTime(inference_stop), audioLength.toPrecision(4));
138+
Ds.DestroyModel(model);
138139
process.exit(0);
139140
});

native_client/javascript/deepspeech.i

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ using namespace node;
2828

2929
// make sure the string returned by SpeechToText is freed
3030
%typemap(newfree) char* "DS_FreeString($1);";
31-
%typemap(newfree) Metadata* "DS_FreeMetadata($1);";
3231

3332
%newobject DS_SpeechToText;
3433
%newobject DS_IntermediateDecode;
@@ -61,19 +60,8 @@ using namespace node;
6160
%append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0));
6261
}
6362

64-
// extend ModelState with a destructor so that DestroyModel will be called
65-
// when the JavaScript object gets finalized.
6663
%nodefaultctor ModelState;
6764
%nodefaultdtor ModelState;
68-
69-
struct ModelState {};
70-
71-
%extend ModelState {
72-
~ModelState() {
73-
DS_DestroyModel($self);
74-
}
75-
}
76-
7765
%nodefaultdtor Metadata;
7866
%nodefaultctor Metadata;
7967
%nodefaultctor MetadataItem;
@@ -94,9 +82,6 @@ struct ModelState {};
9482
v8::Handle<v8::Value> result = SWIGV8_ARRAY_NEW();
9583
return result;
9684
}
97-
~Metadata() {
98-
DS_FreeMetadata($self);
99-
}
10085
}
10186

10287
%rename ("%(strip:[DS_])s") "";

native_client/javascript/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ Model.prototype.finishStreamWithMetadata = function() {
7575
return binding.FinishStreamWithMetadata.apply(null, arguments);
7676
}
7777

78+
function DestroyModel(model) {
79+
return binding.DestroyModel(model._impl);
80+
}
81+
7882
module.exports = {
7983
Model: Model,
80-
printVersions: binding.PrintVersions
84+
printVersions: binding.PrintVersions,
85+
DestroyModel: DestroyModel,
86+
FreeMetadata: binding.FreeMetadata
8187
};

0 commit comments

Comments
 (0)