nodejs native c++ npm module memory error, cairo image processing-Collection of common programming errors

I’ve been bugging TJ on node-canvas about a code speed up I’m working on in a fork of a node module he authored and maintains.

I found Canvas.toBuffer() to be killing our pipeline resources and created an alternative that would simply convert from Canvas into an Image without going through a png buffer/media url. The problem is that cairo is a mysterious beast, and there’s an additional level of concern about memory allocated within node modules as not to get GC’d by mother v8. I’ve added the proper HandleScopes to all required functions which access V8 data.

I was able to test the Canvas.loadImage(image) method thousands of times on my mac setup (6.18), as well as stand alone tests on our ubuntu/production servers running the same version of node. But when the code is run as a background process/server and coordinated by Gearman I’m getting some “interesting” memory/segfaults.

In addition I’m having trouble calling any of the methods of classes defined in node-canvas that aren’t inline within header files. As a side question What’s the best way to create common native source code packages that other node modules can rely on?

I’ve tried recreating the problem and running it with gdb, node_g, and all the node modules built with symbols and debug flags. But the error crops up in a lib outside of the source I can get a stack trace for.

for reference here’s where I call loadImageData and while it runs locally under a variety of conditions, in our production environment when carefully tucked away within a frame server it appears to be causing segfaults (spent the day yesterday trying to gdb node_g our server code but the frame servers are kicked off by gearman… TL;DR didn’t get a root cause stack trace)

https://github.com/victusfate/node-canvas/blob/master/src/Canvas.cc#L497

Handle
 Canvas::LoadImage(const Arguments &args) {
   HandleScope scope;
   LogStream mout(LOG_DEBUG,"node-canvas.paint.ccode.Canvas.LoadImage");    
   mout surface());
    int h = cairo_image_surface_get_height(this->surface());

    img->loadFromDataBuffer(closure->data,w,h);
    mout loadFromDataBuffer(cairo_image_surface_get_data(this->surface()),w,h);
    mout

Originally posted 2013-11-14 03:04:38.