MNN icon indicating copy to clipboard operation
MNN copied to clipboard

readmap崩溃

Open SeanYangyong opened this issue 1 year ago • 5 comments

平台(如果交叉编译请再附上交叉编译目标平台):

Android 小米12 pro

Github版本:

2.9.3版本

编译日志:

崩溃

正常运行一会儿后,很容易发生崩溃。不清楚是哪里的问题。 代码如下:

    MNN::Express::VARP image = MNN::CV::imdecode(srcbuf, MNN::CV::IMREAD_COLOR);
    if (image.get() == nullptr || image == nullptr) {
        MNN_PRINT("decode_image_ is nullptr");
        return nullptr;
    }

    auto img = image->readMap<uint8_t>();

SeanYangyong avatar Sep 10 '24 12:09 SeanYangyong

无标题 在此崩溃前,还打印有个错误,但是image->getInfo()->size并不为0,image并不为空

SeanYangyong avatar Sep 11 '24 02:09 SeanYangyong

完整测试代码是?

jxt1234 avatar Sep 12 '24 08:09 jxt1234

Picture::SPtr decodePicture(void *data, int size) {
    auto pic = std::make_shared<Picture>();
    int x = 0;
    int y = 0;
    int channels = 0;
    int bitcount = 3;
    auto start = utils::CurrentMillionSecond();
    std::vector<uint8_t> srcbuf((uint8_t*)data, (uint8_t*)data + size);
    MNN::Express::VARP image = MNN::CV::imdecode(srcbuf, MNN::CV::IMREAD_COLOR);

    auto img = image->readMap<uint8_t>();
    auto info = image->getInfo();
    int img_data_size = image->getInfo()->size;
    x = info->dim[1];
    y = info->dim[0];
    channels = info->dim[2];
    if (img != nullptr) {
        if (channels == 3) {
            pic->allocData(PICTURE_FORMAT(x, y, PIXFMT_E_RGB24, x * 3, 0, 0, 0));
        } else if (channels == 4) {
            pic->allocData(PICTURE_FORMAT(x, y, PIXFMT_E_RGBA, x * 4, 0, 0, 0));
        }
        memcpy(pic->data(), img, x*y*channels);
    } else {
        loge(TAG"load png failed!");
    }
    srcbuf.clear();
    return pic;
}

起了一个线程,不断decodePicture,将jpg图片的数据解码

SeanYangyong avatar Sep 12 '24 10:09 SeanYangyong

好像是推理和读取解析数据不在同一个线程,就容易导致崩溃。readMap和推理以及内存操作这些是不是最好在同一个线程中?

SeanYangyong avatar Sep 14 '24 03:09 SeanYangyong

  1. Express / Module API 不是线程安全的,不同线程中调用需要绑定同一个 executor https://mnn-docs.readthedocs.io/en/latest/inference/module.html#id1 。
  2. MNN::CV 使用的是 Express ,因此并非线程安全,同样需要先创建一个 Executor ,在调用 MNN::CV 相关函数时绑定该 Executor 。这个 mnn 后面会优化相关 api。

jxt1234 avatar Sep 18 '24 07:09 jxt1234

Marking as stale. No activity in 60 days.

github-actions[bot] avatar Nov 17 '24 09:11 github-actions[bot]