c++ - nodejs native c++ npm模块内存错误,开罗图像处理

标签 c++ node.js image-processing npm

我一直在 node-canvas 上窃听 TJ关于代码加速我正在他创作和维护的 Node 模块的一个分支中工作。

我发现 Canvas.toBuffer() 正在扼杀我们的管道资源,并创建了一个替代方案,它可以简单地从 Canvas 转换为图像,而无需通过 png 缓冲区/媒体 url。问题是 cairo 是一头神秘的野兽,并且对于 Node 模块内分配的内存还有一个额外的担忧,因为它不会被母亲 v8 GC'd。我已将正确的 HandleScopes 添加到访问 V8 数据的所有必需函数中。

我能够在我的 mac 设置 (6.18) 上测试 Canvas.loadImage(image) 方法数千次,以及在运行相同版本 Node 的 ubuntu/生产服务器上进行独立测试。但是当代码作为后台进程/服务器运行并由 Gearman 协调时,我会得到一些“有趣的”内存/段错误。

此外,我在调用 Node Canvas 中定义的任何未内联在头文件中的类的方法时遇到问题。作为一个附带问题创建其他 Node 模块可以依赖的通用 native 源代码包的最佳方法是什么?

我尝试重新创建问题并使用 gdb、node_g 以及使用符号和调试标志构建的所有 Node 模块运行它。但是错误出现在源代码之外的库中,我可以获得堆栈跟踪。

作为引用,我在这里调用 loadImageData,虽然它在各种条件下本地运行,但在我们的生产环境中,当小心地隐藏在框架服务器中时,它似乎会导致段错误(昨天花了一天时间尝试 gdb node_g 我们的服务器代码,但框架服务器由 gearman 启动... TL;DR 没有得到根本原因堆栈跟踪)

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

Handle<Value>
 Canvas::LoadImage(const Arguments &args) {
   HandleScope scope;
   LogStream mout(LOG_DEBUG,"node-canvas.paint.ccode.Canvas.LoadImage");    
   mout << "Canvas::LoadImage top " << LogStream::endl;

   Canvas *canvas = ObjectWrap::Unwrap<Canvas>(args.This());
   if (args.Length() < 1) {
     mout << "Canvas::LoadImage Error requires one argument of Image type " << LogStream::endl;
     return ThrowException(Exception::TypeError(String::New("Canvas::LoadImage requires one argument of Image type")));
   }

   Local<Object> obj = args[0]->ToObject();
   Image *img = ObjectWrap::Unwrap<Image>(obj);
   canvas->loadImageData(img);
   return Undefined();
}  

void Canvas::loadImageData(Image *img) {
  LogStream mout(LOG_DEBUG,"node-canvas.paint.ccode.Canvas.loadImageData");    
  if (this->isPDF()) {
    mout << "Canvas::loadImageData pdf canvas type " << LogStream::endl;
    cairo_surface_finish(this->surface());
    closure_t *closure = (closure_t *) this->closure();

    int w = cairo_image_surface_get_width(this->surface());
    int h = cairo_image_surface_get_height(this->surface());

    img->loadFromDataBuffer(closure->data,w,h);
    mout << "Canvas::loadImageData pdf type, finished loading image" << LogStream::endl;
  }
  else {
    mout << "Canvas::loadImageData data canvas type " << LogStream::endl;
    cairo_surface_flush(this->surface());
    int w = cairo_image_surface_get_width(this->surface());
    int h = cairo_image_surface_get_height(this->surface());

    img->loadFromDataBuffer(cairo_image_surface_get_data(this->surface()),w,h);
    mout << "Canvas::loadImageData image type, finished loading image" << LogStream::endl;
  }   
}

这是 Image 中当前方法的样子(我删除了一些注释掉的日志信息) https://github.com/victusfate/node-canvas/blob/master/src/Image.cc#L240

/*
 * load from data buffer width*height*4 bytes
 */
cairo_status_t
Image::loadFromDataBuffer(uint8_t *buf, int width, int height) {
  this->clearData();
  int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width); // 4*width + ?
  this->_surface = cairo_image_surface_create_for_data(buf,CAIRO_FORMAT_ARGB32,width,height,stride);
  this->data_mode = DATA_IMAGE;
  this->loaded();
  cairo_status_t status = cairo_surface_status(_surface);
  if (status) return status;
  return CAIRO_STATUS_SUCCESS;
}

我们将不胜感激任何帮助、专业提示、帮助或鼓励的话。

来自 google groups

最佳答案

知道了!

我今天正在研究另一个使用 cairomm 的库,并发现从数据缓冲区创建的表面需要这些缓冲区与表面一样长。

http://www.cairographics.org/manual/cairo-Image-Surfaces.html#cairo-image-surface-create-for-data

"为提供的像素数据创建一个图像表面。必须保留输出缓冲区,直到 cairo_surface_t 被破坏或在表面上调用 cairo_surface_finish()。数据的初始内容将用作初始图像内容;如果您想清除缓冲区,则必须明确清除缓冲区,例如使用 cairo_rectangle() 和 cairo_fill()。"

我介绍了一个从临时缓冲区创建的表面。


node-canvas fork 中的简单解决方案:

有一个名为 _data 的成员变量,我可以为其分配一个本地分配的数据缓冲区,只要 cairo 表面存在,它就会一直存在。


解决方案:

将缓冲区复制到表面的一般方法是从缓冲区创建一个临时表面,然后从临时表面绘制到分配的表面上,让 cairo 管理它自己的内存。

使用 c api 到 cairo 来实现看起来像这样。

cairo_surface_t *pTmp = cairo_image_surface_create_for_data (
   data
 , CAIRO_FORMAT_ARGB32
 , width
 , height
 , cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width));

_surface = cairo_image_surface_create ( CAIRO_FORMAT_ARGB32
 , width
 , height);

cairo_t *cr = cairo_create (_surface);
cairo_set_source_surface (cr, pTmp, x, y);
cairo_paint (cr);

关于c++ - nodejs native c++ npm模块内存错误,开罗图像处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10980781/

相关文章:

c++缓冲区溢出或损坏的变量

node.js - Protractor 插件 - 可访问性 - AssertionError : path must be a string

PHP GD 创建一个 PNG 文件,当我尝试在同一文件上使用 imagefronpng() 时失败

image-processing - 带有 SIFT/VLFEAT 的图像描述符

c++ - 等价于 C++ 中来自 Java 的枚举的 .values()

带有对 C++ 回调的字符串引用的 C# 委托(delegate)

C++ 平面数组与多维数组内存占用

node.js - 在 WebStorm 中为 Mocha 运行 typescript 测试

javascript - 如何 promise 我编写的简单异步函数?

image-processing - 如何使用 OpenCV 在角点检测中指定阈值角度?