Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OpenCV error] Failed to allocate XXXXXXX bytes in function 'OutOfMemoryError' #499

Open
OscarHSK opened this issue Jun 8, 2022 · 7 comments

Comments

@OscarHSK
Copy link

OscarHSK commented Jun 8, 2022

I have purchased the Oak-D cam a few weeks ago and with Python, everything goes well. The problem is when I try to run the camera in C++ using Code::Blocks the script builds correctly but when I use the function cv::imshow("name", InCam->getCvFrame()); I got the error "Failed to allocate XXXXXXX bytes in function 'OutOfMemoryError' "

Memoryleak

The codes I tried to run are Mono Preview and RGB Preview and both failed. When I use OpenCV out of this it works well so the problem is not in OpenCV but I don't find any solution to this problem. Is this a common problem?

@themarpe
Copy link
Collaborator

themarpe commented Jun 9, 2022

Hi @OscarHSK
Which library version are you using (you may display this information using env var DEPTHAI_LEVEL=debug).

It seems it might be an issue with incorrectly interpreting the incoming frame, and allocating the size of cv::Mat to be too large.
This should not happen in normal circumstances.

@OscarHSK
Copy link
Author

Hi @themarpe
This is the information displayed with the environmental variabl
Screenshot from 2022-06-10 10-35-37
e

@themarpe
Copy link
Collaborator

Thanks - can you post a log of the run with DEPTHAI_LEVEL=trace as well - that'd give us information of actual messages transferred, which will help in pinpointing the root of the issue.

(Note: please prefer pasting the logs as text instead of pictures / screenshots, as in other case, we would have to manually type over any information / or do OCR on it... - https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question/285557#285557)

@OscarHSK
Copy link
Author

OscarHSK commented Jun 10, 2022

Here it is, sorry about pasting as text before.

[2022-06-10 12:07:09.458] [debug] Library information - version: 2.15.5, commit: cedff3f6419e07dca5fd9c5a087432f665613db8 from 2022-05-28 22:40:44 +0200, build: 2022-06-01 09:26:43 +0000
[2022-06-10 12:07:09.460] [debug] Initialize - finished
[2022-06-10 12:07:09.463] [debug] Device - OpenVINO version: 2021.4
[2022-06-10 12:07:09.670] [debug] Resources - Archive 'depthai-bootloader-fwp-0.0.18+c555ac2fb184b801291c95f7f73d23bf4dd42cf1.tar.xz' open: 5ms, archive read: 205ms
[2022-06-10 12:07:10.525] [debug] Resources - Archive 'depthai-device-fwp-12ab42f2923f30cedbf129fbb765ae7ca973bdba.tar.xz' open: 6ms, archive read: 1059ms
[2022-06-10 12:07:12.092] [trace] RPC: [1,1,10182484315255513117,[0]]
[2022-06-10 12:07:12.105] [trace] RPC: [1,1,5804304869041345055,[1.0]]
[2022-06-10 12:07:12.111] [trace] Log vector decoded, size: 3
[18443010C1591EF500] [249.938] [system] [info] Memory Usage - DDR: 0.12 / 341.19 MiB, CMX: 2.05 / 2.50 MiB, LeonOS Heap: 7.22 / 78.13 MiB, LeonRT Heap: 2.88 / 41.43 MiB
[18443010C1591EF500] [249.938] [system] [info] Temperatures - Average: 35.82 °C, CSS: 36.30 °C, MSS 36.30 °C, UPA: 35.35 °C, DSS: 35.35 °C
[18443010C1591EF500] [249.938] [system] [info] Cpu Usage - LeonOS 10.73%, LeonRT: 2.05%
[2022-06-10 12:07:12.118] [trace] RPC: [1,1,17425566508637143278,null]
[2022-06-10 12:07:12.131] [trace] RPC: [1,1,16527326580805871264,[{"connections":[{"node1Id":2,"node1Output":"preview","node1OutputGroup":"","node2Id":3,"node2Input":"in","node2InputGroup":""},{"node1Id":0,"node1Output":"out","node1OutputGroup":"","node2Id":1,"node2Input":"in","node2InputGroup":""}],"globalProperties":{"calibData":null,"cameraTuningBlobSize":null,"cameraTuningBlobUri":"","leonCssFrequencyHz":700000000.0,"leonMssFrequencyHz":700000000.0,"pipelineName":null,"pipelineVersion":null,"xlinkChunkSize":-1},"nodes":[[0,{"id":0,"ioInfo":[[["","out"],{"blocking":false,"group":"","name":"out","queueSize":8,"type":0,"waitForMessage":false}]],"name":"SystemLogger","properties":[185,1,136,0,0,128,63]}],[1,{"id":1,"ioInfo":[[["","in"],{"blocking":true,"group":"","name":"in","queueSize":8,"type":3,"waitForMessage":true}]],"name":"XLinkOut","properties":[185,3,136,0,0,128,191,189,7,115,121,115,105,110,102,111,0]}],[2,{"id":2,"ioInfo":[[["","preview"],{"blocking":false,"group":"","name":"preview","queueSize":8,"type":0,"waitForMessage":false}],[["","still"],{"blocking":false,"group":"","name":"still","queueSize":8,"type":0,"waitForMessage":false}],[["","isp"],{"blocking":false,"group":"","name":"isp","queueSize":8,"type":0,"waitForMessage":false}],[["","video"],{"blocking":false,"group":"","name":"video","queueSize":8,"type":0,"waitForMessage":false}],[["","raw"],{"blocking":false,"group":"","name":"raw","queueSize":8,"type":0,"waitForMessage":false}],[["","inputConfig"],{"blocking":false,"group":"","name":"inputConfig","queueSize":8,"type":3,"waitForMessage":false}],[["","inputControl"],{"blocking":true,"group":"","name":"inputControl","queueSize":8,"type":3,"waitForMessage":false}]],"name":"ColorCamera","properties":[185,18,185,20,0,3,0,185,3,0,0,0,185,5,0,0,0,0,0,185,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,1,0,0,100,100,255,255,255,255,0,136,0,0,128,63,136,0,0,128,191,136,0,0,128,191,1,185,4,0,0,0,0]}],[3,{"id":3,"ioInfo":[[["","in"],{"blocking":true,"group":"","name":"in","queueSize":8,"type":3,"waitForMessage":true}]],"name":"XLinkOut","properties":[185,3,136,0,0,128,191,189,3,114,103,98,0]}]]}]]
[2022-06-10 12:07:12.143] [trace] RPC: [1,1,10180360702496156555,null]
[2022-06-10 12:07:12.145] [trace] RPC: [1,1,14047900442330284907,null]
[2022-06-10 12:07:12.185] [trace] Log vector decoded, size: 1
[18443010C1591EF500] [249.978] [system] [info] ImageManip internal buffer size '80640'B, shave buffer size '34816'B
[2022-06-10 12:07:12.189] [trace] RPC: [1,1,8959630473823391071,null]
[2022-06-10 12:07:12.215] [trace] Log vector decoded, size: 2
[18443010C1591EF500] [249.978] [system] [info] SIPP (Signal Image Processing Pipeline) internal buffer size '16384'B
[18443010C1591EF500] [250.013] [system] [info] ColorCamera allocated resources: no shaves; cmx slices: [13-15] 
ImageManip allocated resources: shaves: [15-15] no cmx slices. 

[2022-06-10 12:07:12.266] [trace] Received message from device (sysinfo) - parsing time: 571µs, data size: 0, object type: 7 object data: 
0000: b9 07 b9 03 86 7f b6 8b 13 86 00 44 c7 01 86 7f fa 52 15 b9 03 86 00 10 05 00 86 00 f0 22 00 86
0020: 00 00 28 00 b9 03 86 30 8e 87 03 86 c8 7b 5a 01 86 f8 09 e2 04 b9 03 86 30 9f 61 02 86 48 3d 35
0040: 00 86 78 dc 96 02 b9 02 88 98 ca db 3d 85 e8 03 b9 02 88 a0 9b a7 3c 85 e8 03 b9 05 88 31 c7 17
0060: 42 88 46 3f 10 42 88 cd 5a 0e 42 88 1d 14 13 42 88 58 5d 12 42
Ddr used / total - 28.45 / 341.19 MiB
Cmx used / total - 2.18 / 2.50 MiB
LeonCss heap used / total - 21.66 / 78.13 MiB
LeonMss heap used / total - 3.33 / 41.43 MiB
Chip temperature - average: 36.59, css: 37.94, mss: 36.06, upa: 35.59, dss: 36.77
Cpu usage - Leon CSS: 10.73 %, Leon MSS: 2.05 %
----------------------------------------
[2022-06-10 12:07:12.289] [trace] Received message from device (rgb) - parsing time: 413µs, data size: 30000, object type: 1 object data: 
0000: b9 06 b9 08 07 64 64 64 01 00 81 10 27 81 20 4e 00 00 00 b9 02 85 a7 1d 86 51 71 fb 02 b9 02 85
0020: fa 00 86 59 70 8b 05
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.5-dev) /opt/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 124131365788560 bytes in function 'OutOfMemoryError'

Stack trace (most recent call last):
#14   Object "[0xffffffffffffffff]", at 0xffffffffffffffff, in 
#13   Object "/home/oscar/OakD/bin/Debug/OakD", at 0x55610caac70d, in 
#12   Object "/lib/x86_64-linux-gnu/libc.so.6", at 0x7f042c82a082, in __libc_start_main
#11   Object "/home/oscar/OakD/bin/Debug/OakD", at 0x55610caace60, in 
#10   Object "/home/oscar/depthai-core/[install]/lib/libdepthai-opencv.so", at 0x7f042eb5c804, in dai::ImgFrame::getCvFrame()
#9    Object "/usr/local/lib/libopencv_core.so.405", at 0x7f042ce2eeea, in cv::merge(cv::_InputArray const&, cv::_OutputArray const&)
#8    Object "/usr/local/lib/libopencv_core.so.405", at 0x7f042ce2e699, in cv::merge(cv::Mat const*, unsigned long, cv::_OutputArray const&)
#7    Object "/usr/local/lib/libopencv_core.so.405", at 0x7f042ce22d5a, in cv::_OutputArray::create(int, int const*, int, int, bool, cv::_OutputArray::DepthMask) const
#6    Object "/usr/local/lib/libopencv_core.so.405", at 0x7f042ccc57c0, in 
#5    Object "/lib/x86_64-linux-gnu/libstdc++.so.6", at 0x7f042cabf6fc, in __cxa_rethrow
#4    Object "/lib/x86_64-linux-gnu/libstdc++.so.6", at 0x7f042cabf3f6, in std::terminate()
#3    Object "/lib/x86_64-linux-gnu/libstdc++.so.6", at 0x7f042cabf38b, in 
#2    Object "/lib/x86_64-linux-gnu/libstdc++.so.6", at 0x7f042cab3910, in 
#1    Object "/lib/x86_64-linux-gnu/libc.so.6", at 0x7f042c828858, in abort
#0    Object "/lib/x86_64-linux-gnu/libc.so.6", at 0x7f042c84900b, in gsignal
Aborted (Signal sent by tkill() 10069 1000)
Aborted (core dumped)

Process returned 134 (0x86)   execution time : 3.301 s
Press ENTER to continue.

@themarpe
Copy link
Collaborator

The following does not reproduce it on 2.15.5:

#include <iostream>
#include "depthai/depthai.hpp"
int main() {
    const std::vector<uint8_t> ser = {0xb9, 0x06, 0xb9, 0x08, 0x07, 0x64, 0x64, 0x64, 0x01, 0x00, 0x81, 0x10, 0x27, 0x81, 0x20, 0x4e, 0x00, 0x00, 0x00, 0xb9,
                                      0x02, 0x85, 0xa7, 0x1d, 0x86, 0x51, 0x71, 0xfb, 0x02, 0xb9, 0x02, 0x85, 0xfa, 0x00, 0x86, 0x59, 0x70, 0x8b, 0x05};

    auto imgFrm = std::make_shared<dai::RawImgFrame>();
    dai::utility::deserialize(ser, *imgFrm);

    nlohmann::json j = *imgFrm;
    std::cout << j.dump(4) << std::endl;

    imgFrm->data.resize(100 * 100 * 3);

    dai::ImgFrame frm(imgFrm);
    frm.getCvFrame();

    return 0;
}

Contents of ImgFrame "metadata"

{
    "category": 0,
    "fb": {
        "bytesPP": 1,
        "height": 100,
        "p1Offset": 0,
        "p2Offset": 10000,
        "p3Offset": 20000,
        "stride": 100,
        "type": 7,
        "width": 100
    },
    "instanceNum": 0,
    "sequenceNum": 0,
    "ts": {
        "nsec": 50032977,
        "sec": 7591
    },
    "tsDevice": {
        "nsec": 93024345,
        "sec": 250
    }
}

It works as expected.

@OscarHSK Can you try some example first and try to see if all works as expected (eg. rgb_preview.cpp). It seems you are using a custom app in which this happens. Also make sure you aren't modifying the shared messages, as they aren't protected from concurrent write access.

Also how did you integrate core into your own project? CMake or some other way? Make sure all defines are set properly or switch to 2.16.0 for less issues in this regard

@OscarHSK
Copy link
Author

@themarpe I tried with rgb_preview.cpp and happens the same.

I integrated the project as the documentation says, first building as a dynamic library and then installing it.

@themarpe
Copy link
Collaborator

themarpe commented Jun 13, 2022

@OscarHSK
Can you try building the examples as part of the build as well and trying it those? Wondering if there are some issues with integration
https://github.com/luxonis/depthai-core#running-examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants