0

I'm working with libuv (https://github.com/joyent/libuv) and nodejs v12. I I write native module, in this module i have struct:

    struct Work_req
    {
        const char *data;
        size_t data_length;
        Isolate* isolate;
        unsigned int callback_id;

        Persistent<Function> callback;
    };

Then i try to pass this structure to work:

     Work_req* request = new Work_req;
     request->data = buf->base;
     request->data_length = (size_t)nread;
     request->isolate = env->isolate();
     request->callback_id = callback_id->ToNumber()->Value();

     uv_work_t *req = new uv_work_t;
     req->data = request;

     uv_queue_work(env->event_loop(), req, findCallback, after_findCallback);

In the end of this code i passed work to loop and all is ok. But when i'm trying to read data from uv_work_t *req in findCallback function, i get strange symbols there:

      Work_req *s = ((struct Work_req*)req->data);
      printf(s>data); //print char array from structure here

I see something like this:

����g�Kack_id":1,"error":null,"response":{"type":"test"}}�����

How i can fix it?

Thanks a lot.

user4176305
  • 141
  • 7
  • This code appears to queue to an async-callback. Since you're only saving a pointer value to `req->data` is may be in your best interest to know for sure the *source* of that value (`buf`) isn't freeing it, using it for something else etc. Something tells me it may be long-gone by the time your callback gets around to handling it. – WhozCraig May 17 '15 at 03:16
  • My guess would be, `buf->base` has been destroyed by the time the callback runs, leaving `s->data` a dangling pointer. – Igor Tandetnik May 17 '15 at 03:17
  • I found place where buf->base destroyed before work running. Thanks! – user4176305 May 17 '15 at 03:28

0 Answers0