{"id":1368,"date":"2022-08-30T15:15:57","date_gmt":"2022-08-30T15:15:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/14\/nodejs-native-c-npm-module-memory-error-cairo-image-processing-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:57","modified_gmt":"2022-08-30T15:15:57","slug":"nodejs-native-c-npm-module-memory-error-cairo-image-processing-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/nodejs-native-c-npm-module-memory-error-cairo-image-processing-collection-of-common-programming-errors\/","title":{"rendered":"nodejs native c++ npm module memory error, cairo image processing-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve been bugging TJ on node-canvas about a code speed up I&#8217;m working on in a fork of a node module he authored and maintains.<\/p>\n<p>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&#8217;s an additional level of concern about memory allocated within node modules as not to get GC&#8217;d by mother v8. I&#8217;ve added the proper HandleScopes to all required functions which access V8 data.<\/p>\n<p>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&#8217;m getting some &#8220;interesting&#8221; memory\/segfaults.<\/p>\n<p><strong>In addition I&#8217;m having trouble calling any of the methods of classes defined in node-canvas that aren&#8217;t inline within header files<\/strong>. As a side question <em>What&#8217;s the best way to create common native source code packages that other node modules can rely on?<\/em><\/p>\n<p>I&#8217;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.<\/p>\n<p>for reference here&#8217;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&#8230; TL;DR didn&#8217;t get a root cause stack trace)<\/p>\n<p>https:\/\/github.com\/victusfate\/node-canvas\/blob\/master\/src\/Canvas.cc#L497<\/p>\n<pre><code>Handle\n Canvas::LoadImage(const Arguments &amp;args) {\n   HandleScope scope;\n   LogStream mout(LOG_DEBUG,\"node-canvas.paint.ccode.Canvas.LoadImage\");    \n   mout surface());\n    int h = cairo_image_surface_get_height(this-&gt;surface());\n\n    img-&gt;loadFromDataBuffer(closure-&gt;data,w,h);\n    mout loadFromDataBuffer(cairo_image_surface_get_data(this-&gt;surface()),w,h);\n    mout<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-14 03:04:38. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been bugging TJ on node-canvas about a code speed up I&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1368","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1368"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1368\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}