@@ -39,6 +39,12 @@ void ErrorCodeToString(const char* prefix, int errorCode, char *errorStr) {
39
39
}
40
40
}
41
41
42
+ void AsyncCloseCallback (uv_handle_t * handle)
43
+ {
44
+ uv_async_t * async = (uv_async_t *)handle;
45
+ delete async;
46
+ }
47
+
42
48
void EIO_Open (uv_work_t * req) {
43
49
OpenBaton* data = static_cast <OpenBaton*>(req->data );
44
50
@@ -293,9 +299,13 @@ NAN_METHOD(Write) {
293
299
baton->offset = 0 ;
294
300
baton->callback .Reset (info[2 ].As <v8::Function>());
295
301
baton->complete = false ;
302
+
303
+ uv_async_t * async = new uv_async_t ;
304
+ uv_async_init (uv_default_loop (), async, EIO_AfterWrite);
305
+ async->data = baton;
296
306
// WriteFileEx requires a thread that can block. Create a new thread to
297
307
// run the write operation, saving the handle so it can be deallocated later.
298
- baton->hThread = CreateThread (NULL , 0 , WriteThread, baton , 0 , NULL );
308
+ baton->hThread = CreateThread (NULL , 0 , WriteThread, async , 0 , NULL );
299
309
}
300
310
301
311
void __stdcall WriteIOCompletion (DWORD errorCode, DWORD bytesTransferred, OVERLAPPED* ov) {
@@ -316,7 +326,8 @@ void __stdcall WriteIOCompletion(DWORD errorCode, DWORD bytesTransferred, OVERLA
316
326
}
317
327
318
328
DWORD __stdcall WriteThread (LPVOID param) {
319
- WriteBaton* baton = static_cast <WriteBaton*>(param);
329
+ uv_async_t * async = static_cast <uv_async_t *>(param);
330
+ WriteBaton* baton = static_cast <WriteBaton*>(async->data );
320
331
321
332
OVERLAPPED* ov = new OVERLAPPED;
322
333
memset (ov, 0 , sizeof (OVERLAPPED));
@@ -338,9 +349,6 @@ DWORD __stdcall WriteThread(LPVOID param) {
338
349
}
339
350
delete ov;
340
351
// Signal the main thread to run the callback.
341
- uv_async_t * async = new uv_async_t ;
342
- uv_async_init (uv_default_loop (), async, EIO_AfterWrite);
343
- async->data = baton;
344
352
uv_async_send (async);
345
353
ExitThread (0 );
346
354
}
@@ -350,7 +358,7 @@ void EIO_AfterWrite(uv_async_t* req) {
350
358
WriteBaton* baton = static_cast <WriteBaton*>(req->data );
351
359
WaitForSingleObject (baton->hThread , INFINITE);
352
360
CloseHandle (baton->hThread );
353
- delete req;
361
+ uv_close (( uv_handle_t *) req, AsyncCloseCallback) ;
354
362
355
363
v8::Local<v8::Value> argv[1 ];
356
364
if (baton->errorString [0 ]) {
@@ -411,6 +419,10 @@ NAN_METHOD(Read) {
411
419
baton->bufferData = node::Buffer::Data (buffer);
412
420
baton->callback .Reset (info[4 ].As <v8::Function>());
413
421
baton->complete = false ;
422
+
423
+ uv_async_t * async = new uv_async_t ;
424
+ uv_async_init (uv_default_loop (), async, EIO_AfterRead);
425
+ async->data = baton;
414
426
// ReadFileEx requires a thread that can block. Create a new thread to
415
427
// run the read operation, saving the handle so it can be deallocated later.
416
428
baton->hThread = CreateThread (NULL , 0 , ReadThread, baton, 0 , NULL );
@@ -482,7 +494,8 @@ void __stdcall ReadIOCompletion(DWORD errorCode, DWORD bytesTransferred, OVERLAP
482
494
}
483
495
484
496
DWORD __stdcall ReadThread (LPVOID param) {
485
- ReadBaton* baton = static_cast <ReadBaton*>(param);
497
+ uv_async_t * async = static_cast <uv_async_t *>(param);
498
+ ReadBaton* baton = static_cast <ReadBaton*>(async->data );
486
499
DWORD lastError;
487
500
488
501
OVERLAPPED* ov = new OVERLAPPED;
@@ -516,9 +529,6 @@ DWORD __stdcall ReadThread(LPVOID param) {
516
529
}
517
530
delete ov;
518
531
// Signal the main thread to run the callback.
519
- uv_async_t * async = new uv_async_t ;
520
- uv_async_init (uv_default_loop (), async, EIO_AfterRead);
521
- async->data = baton;
522
532
uv_async_send (async);
523
533
ExitThread (0 );
524
534
}
@@ -528,7 +538,7 @@ void EIO_AfterRead(uv_async_t* req) {
528
538
ReadBaton* baton = static_cast <ReadBaton*>(req->data );
529
539
WaitForSingleObject (baton->hThread , INFINITE);
530
540
CloseHandle (baton->hThread );
531
- delete req;
541
+ uv_close (( uv_handle_t *) req, AsyncCloseCallback) ;
532
542
533
543
v8::Local<v8::Value> argv[2 ];
534
544
if (baton->errorString [0 ]) {
0 commit comments