Segmentation Fault When Extracting Frames
| Required Info | |
|---|---|
| Camera Model | SR300 |
| Firmware Version | 3.15.0.0 |
| Operating System & Version | Ubuntu 16.04 |
| Kernel Version (Linux Only) | 4.4.0-101-generic |
I ran 4 SR300 cameras on one computer and utilized color, depth and ir streams at full resolution. After a while, I came across the following error (can replicate but not deterministic):
Thread 1 "python3.6" received signal SIGSEGV, Segmentation fault. 0x00007fffccdc99f1 in librealsense::frame::release (this=0xfd3558) at /home/arb/librealsense/src/archive.cpp:273 273 owner->unpublish_frame(this);
Backtrace:
#0 0x00007fffccdc99f1 in librealsense::frame::release (this=0xfd3558) at /home/arb/librealsense/src/archive.cpp:273
No locals.
#1 0x00007fffccf1fe6e in rs2_release_frame (frame=0xfd3558) at /home/arb/librealsense/src/rs.cpp:813
FUNCTION = "rs2_release_frame"
PRETTY_FUNCTION = "void rs2_release_frame(rs2_frame*)"
#2 0x00007fffcd7275ee in rs2::frame::~frame (this=
The library is compiled from source. Realsense Viewer works well. Here is the source code:
for (...){ rs2::frameset frameset = pipe.wait_for_frames();
auto color_frame = frameset.get_color_frame();
auto depth_frame = frameset.get_depth_frame();
auto ir_frame = frameset.first_or_default(RS2_STREAM_INFRARED);
...
}
All four cameras are run in separate Python3.6 interpreter sessions/processes.
Whenever this error happens, it shows owner is a NULL pointer.
(gdb) p owner $1 = std::shared_ptr (empty) 0x0
I also have a backtrace which starts from the v4l2 backend:
#0 0x00007fffccdc99f1 in librealsense::frame::release (this=0xffa680) at /home/arb/librealsense/src/archive.cpp:273
#1 0x00007fffccf6e739 in librealsense::frame_holder::operator=(librealsense::frame_holder&&) (this=0x7fffbc04ea38,
other=<unknown type in /usr/local/lib/librealsense2.so.2, CU 0x9e9be9, DIE 0xa31738>) at /home/arb/librealsense/src/types.cpp:75
#2 0x00007fffccd6d344 in librealsense::pipeline_processing_block::handle_frame (this=0xf49620, frame=..., source=0xf49740)
at /home/arb/librealsense/src/pipeline.cpp:34
#3 0x00007fffccd6cd90 in librealsense::pipeline_processing_block::pipeline_processing_block(std::vector<int, std::allocator
Hi @marvin-hao ,
Which version are you using?
@zivsha
In master branch. The most recent log from my local git repo is:
commit 5fb58db832d3fae75c19ba06689e8de53f5a9add Merge: df6cd79 d346693 Author: Sergey Dorodnicov [email protected] Date: Fri Dec 1 03:32:48 2017 -0800
Merge pull request #871 from IntelRealSense/python-doc
Updating python documentation
I added NULL pointer checking for the member variable owner of realsense::frame in src/archive.cpp: void frame::release() and src/source.cpp: void frame_source::invoke_callback(frame_holder frame) const. It works well so far, but I'm not sure if this will cause potential memory leak or further hidden bugs.
Can you please share a code snippet / sample that reproduces this issue so we can try it?
Sorry, most of the confidential code interleaves within the camera control code, but I can share the control flow. We wrote a Python wrapper of rs2 and we run 4 CPython interpreter sessions on a single computer.
- Camera initialization:
try {
self->pipe = rs2::pipeline();
if (!setup_streams(self))
return NULL;
self->pipe_profile = self->pipe.start(self->conf);
if (!setup_intrinsics(self))
return NULL;
self->dev = self->pipe_profile.get_device();
std::vector<rs2::sensor> sensors = self->dev.query_sensors();
// For SR300
if (sensors.size() != 2) {
PyErr_SetString(PyExc_RuntimeError, "...");
return NULL;
}
if (self->depth_sensor = sensors[0].as<rs2::depth_sensor>()){
self->depth_scale = self->depth_sensor.get_depth_scale();
} else {
PyErr_SetString(PyExc_RuntimeError, "...");
return NULL;
}
// RGB Camera
if (self -> roi_sensor = sensors[1].as<rs2::roi_sensor>()){}
else {
PyErr_SetString(PyExc_RuntimeError, "...");
return NULL;
}
} catch (rs2::error e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
}
- Streaming
In an infinite loop:
for (uint i = 0; i < maxFrames; ++i){
rs2::frameset frameset = pipe.wait_for_frames();
try {
auto color_frame = frameset.get_color_frame();
auto depth_frame = frameset.get_depth_frame();
auto ir_frame = frameset.first_or_default(RS2_STREAM_INFRARED);
// If come across bad frames, decrease the counter by 1 and continue.
if (
!ir_frame || ir_frame.get_data() == NULL ||
!color_frame || color_frame.get_data() == NULL ||
!depth_frame || depth_frame.get_data() == NULL) {
--i;
continue;
}
CompFrame memData = {
(uint8_t *)(color_frame.get_data()),
(uint16_t *)(depth_frame.get_data()),
(uint8_t *)(ir_frame.get_data())
};
... (calculation that uses intrinsic and extrinsic)
} catch (std::exception &e) {
throw e;
}
}
We found after we patched the source files I commented before, there's no more segmentation fault. My concern is that if simply skipping null owner will cause a memory leak or other bugs.
Hi @marvin-hao
I couldn't find the exact problem in the code, but the symptoms you describe are indicative of frame object being released more then once. While this should not happen when using rs2::frame class, if you do use some of the C APIs directly this would be the place to review.
we do use some C APIs like rs2_deproject_pixel_to_point to align frames. Is there any available C++ API exposed already? We did use C++ API to align in rs1.
There is no limitation to mix C and C++ APIs, however, my concern is that the function rs2_release_frame is being called twice on the same frame.
There is a useful CMake flag -DTRACE_API=true that would make such situation very visible in the log.
@marvin-hao please open-new issue if you reproduce this problem (closing for now)
I also run into Segmentation fault error. Did you solve this error by patching src/archive.cpp and src/source.cpp? @marvin-hao
Could you please show your solution? Although you have shared your Camera initialization code and Streaming code above, I still confused about how to edit src/archive.cpp and src/source.cpp.
I am puzzled by this problem for several days, my detailed situation is here. Could you provide your solution? Thanks in advance.