File tree Expand file tree Collapse file tree 3 files changed +27
-0
lines changed Expand file tree Collapse file tree 3 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env node
2+
13const fs = require ( 'fs' ) ;
24const path = require ( 'path' ) ;
35const framework = require ( './framework' ) ;
Original file line number Diff line number Diff line change @@ -50,6 +50,10 @@ std::vector<Hello *> take_and_return_ptr_vector(const std::vector<Hello *> &inpu
5050std::shared_ptr<Hello> take_and_return_shared_ptr (const std::shared_ptr<Hello> in) { return in; }
5151std::shared_ptr<Hello> make_shared_ptr (std::string name) { return std::make_shared<Hello>(name); }
5252
53+ std::string take_shared_ptr (std::shared_ptr<Hello> in) {
54+ return in->Greet (" Citizen" );
55+ }
56+
5357NOBIND_MODULE (stress, m) {
5458 m.def <Hello>(" Hello" )
5559 .cons <std::string &>()
@@ -78,4 +82,5 @@ NOBIND_MODULE(stress, m) {
7882
7983 m.def <&take_and_return_shared_ptr, Nobind::ReturnAsync>(" take_and_return_shared_ptr" );
8084 m.def <&make_shared_ptr, Nobind::ReturnAsync>(" make_shared_ptr" );
85+ m.def <&take_shared_ptr, Nobind::ReturnAsync>(" take_shared_ptr" );
8186}
Original file line number Diff line number Diff line change @@ -126,6 +126,7 @@ describe('stress tests', function () {
126126 hellos [ pick ] = await dll . take_and_return_shared_ptr ( hello ) ;
127127 }
128128 } ) ;
129+
129130 it ( 'shared_ptr (start with a shared pointer)' , async ( ) => {
130131 // In this case JavaScript will always hold smart pointers
131132 const hellos = await Promise . all ( ( new Array ( 1000 ) ) . fill ( dll . make_shared_ptr ( 'Merkwürdigliebe' ) , 0 , 1000 ) ) ;
@@ -138,4 +139,23 @@ describe('stress tests', function () {
138139 hellos [ pick ] = await dll . take_and_return_shared_ptr ( hello ) ;
139140 }
140141 } ) ;
142+
143+ it ( 'test shared_ptr destruction in background threads' , async ( ) => {
144+ // https://github.com/mmomtchev/nobind/issues/56
145+ // Make nobind create shared pointers out of anonymous
146+ // JS objects and process them asynchronously to ensure
147+ // that the JS reference is never released in a background
148+ // thread
149+ const q = [ ] ;
150+ for ( let i = 0 ; i < 5e4 ; i ++ ) {
151+ const pick = Math . floor ( Math . random ( ) * 1000 ) ;
152+ q . push ( dll . take_shared_ptr ( new dll . Hello ( 'Rasczak' ) ) ) ;
153+ }
154+
155+ const r = await Promise . all ( q ) ;
156+ assert . lengthOf ( r , 5e4 ) ;
157+ for ( const s of r ) {
158+ assert . strictEqual ( s , 'hello Citizen Rasczak' ) ;
159+ }
160+ } ) ;
141161} ) ;
You can’t perform that action at this time.
0 commit comments