@@ -347,7 +347,7 @@ void Blob::Reader::Pull(const FunctionCallbackInfo<Value>& args) {
347347 }
348348
349349 struct Impl {
350- BaseObjectPtr <Blob::Reader> reader;
350+ BaseObjectWeakPtr <Blob::Reader> reader;
351351 Global<Function> callback;
352352 Environment* env;
353353 std::vector<DataQueue::Vec> vecs;
@@ -361,15 +361,18 @@ void Blob::Reader::Pull(const FunctionCallbackInfo<Value>& args) {
361361 // EDIT(martenrichter) We use a shared_ptr instead, as the previous
362362 // implementation, with am ommrt unique_ptr did not allow to call next twice
363363 // as impl is gone after the first call.
364+ // Also the reference to reader should be a weak pointer, as if it is
365+ // collected we should not be interested in the result of a call to next
364366 std::shared_ptr<Impl> impl = std::make_shared<Impl>();
365- impl->reader = BaseObjectPtr <Blob::Reader>(reader);
367+ impl->reader = BaseObjectWeakPtr <Blob::Reader>(reader);
366368 impl->callback .Reset (env->isolate (), fn);
367369 impl->env = env;
368370
369371 auto next = [impl](int status,
370372 const DataQueue::Vec* vecs,
371373 size_t count,
372374 bob::Done doneCb) mutable {
375+ assert (!impl->reader );
373376 Environment* env = impl->env ;
374377 HandleScope handleScope (env->isolate ());
375378 Local<Function> fn = impl->callback .Get (env->isolate ());
@@ -403,6 +406,7 @@ void Blob::Reader::Pull(const FunctionCallbackInfo<Value>& args) {
403406 for (size_t n = 0 ; n < count; n++) impl->byte_count += vecs[n].len ;
404407 }
405408 };
409+ assert (!impl->reader );
406410 status = impl->reader ->inner_ ->Pull (
407411 std::move (snext), node::bob::OPTIONS_SYNC, nullptr , 0 );
408412 }
@@ -429,12 +433,14 @@ void Blob::Reader::Pull(const FunctionCallbackInfo<Value>& args) {
429433 impl->dones .end (),
430434 [](bob::Done& done) { std::move (done)(0 ); });
431435 impl->dones .clear ();
436+ assert (!impl->reader );
432437 Local<Value> argv[2 ] = {Uint32::New (env->isolate (), bob::STATUS_CONTINUE),
433438 ArrayBuffer::New (env->isolate (), store)};
434439 impl->reader ->MakeCallback (fn, arraysize (argv), argv);
435440 return ;
436441 }
437442 impl->dones .clear (); // should not be necessary?
443+ assert (!impl->reader );
438444
439445 Local<Value> argv[2 ] = {
440446 Int32::New (env->isolate (), status),
0 commit comments