Permalink
Browse files

don't call Buffer::Data() on the thread pool

Fixes #95.
  • Loading branch information...
1 parent 3f8e735 commit 41ea6ee830a957afa8b6c502d6f303d439bca583 @TooTallNate TooTallNate committed Dec 15, 2012
Showing with 20 additions and 26 deletions.
  1. +7 −0 lib/_foreign_function.js
  2. +9 −22 src/ffi.cc
  3. +4 −4 src/ffi.h
View
@@ -101,6 +101,13 @@ function ForeignFunction (cif, funcPtr, returnType, argTypes) {
// invoke the `ffi_call()` function asynchronously
bindings.ffi_call_async(cif, funcPtr, result, argsList, function (err) {
+ // make sure that the 4 Buffers passed in above don't get GC'd while we're
+ // doing work on the thread pool...
+ cif = cif;
+ funcPtr = funcPtr;
+ argsList = argsList;
+
+ // now invoke the user-provided callback function
if (err) {
callback(err)
} else {
View
@@ -310,10 +310,10 @@ Handle<Value> FFI::FFICallAsync(const Arguments& args) {
p->result = FFI_OK;
// store a persistent references to all the Buffers and the callback function
- p->cif = Persistent<Object>::New(args[0]->ToObject());
- p->fn = Persistent<Object>::New(args[1]->ToObject());
- p->res = Persistent<Object>::New(args[2]->ToObject());
- p->argv = Persistent<Object>::New(args[3]->ToObject());
+ p->cif = Buffer::Data(args[0]->ToObject());
+ p->fn = Buffer::Data(args[1]->ToObject());
+ p->res = Buffer::Data(args[2]->ToObject());
+ p->argv = Buffer::Data(args[3]->ToObject());
Local<Function> callback = Local<Function>::Cast(args[4]);
p->callback = Persistent<Function>::New(callback);
@@ -335,19 +335,14 @@ Handle<Value> FFI::FFICallAsync(const Arguments& args) {
void FFI::AsyncFFICall(uv_work_t *req) {
AsyncCallParams *p = (AsyncCallParams *)req->data;
- char *cif = Buffer::Data(p->cif);
- char *fn = Buffer::Data(p->fn);
- char *res = Buffer::Data(p->res);
- char *argv = Buffer::Data(p->argv);
-
#if __OBJC__ || __OBJC2__
@try {
#endif
ffi_call(
- (ffi_cif *)cif,
- FFI_FN(fn),
- (void *)res,
- (void **)argv
+ (ffi_cif *)p->cif,
+ FFI_FN(p->fn),
+ (void *)p->res,
+ (void **)p->argv
);
#if __OBJC__ || __OBJC2__
} @catch (id ex) {
@@ -378,15 +373,7 @@ void FFI::FinishAsyncFFICall(uv_work_t *req) {
// invoke the registered callback function
p->callback->Call(Context::GetCurrent()->Global(), 1, argv);
- // dispose of our persistent handles to the Buffers and callback function
- p->cif.Dispose();
- p->cif.Clear();
- p->fn.Dispose();
- p->fn.Clear();
- p->res.Dispose();
- p->res.Clear();
- p->argv.Dispose();
- p->argv.Clear();
+ // dispose of our persistent handle to the callback function
p->callback.Dispose();
p->callback.Clear();
View
@@ -41,10 +41,10 @@ class AsyncCallParams {
public:
ffi_status result;
char *err;
- Persistent<Object> cif;
- Persistent<Object> fn;
- Persistent<Object> res;
- Persistent<Object> argv;
+ char *cif;
+ char *fn;
+ char *res;
+ char *argv;
Persistent<Function> callback;
};

0 comments on commit 41ea6ee

Please sign in to comment.