0

In my code I'm encountering a situation where it is running for few hours but crashing at random points after that(SIGSEGV, Segmentation fault). Whenever crash happens, the cv::Mat involved(often different) will have the u parameter as a nullptr. (Link to what u is: https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a2742469fe595e1b9036f60d752d08461)

So I'm wondering what this u being a nullptr actually means and if this is the cause for crashing? This is confusing because u is 0x0 or nullptr at other points during execution as well and not just when it crashes.

An example code of how I'm using the mats and causing the u to become 0x0:

#include <opencv2/opencv.hpp>

int main()
{
    cv::Mat main_data = cv::Mat::zeros(10, 10, CV_8UC3);
    cv::Mat buffer = cv::Mat::zeros(100, 100, CV_8UC3);
    cv::Mat valid_data = buffer;

    std::cout << "before: " << valid_data.u << std::endl;

    memcpy(buffer.data, main_data.data, main_data.rows*main_data.step);
    valid_data = cv::Mat(main_data.rows, main_data.cols, main_data.type(), buffer.data, main_data.step[0]);
    std::cout << "after: " << valid_data.u << std::endl;

    return 0;
}

I have multiple threads and I'm using the above along with locks for sharing data between them.

romil
  • 38
  • 1
  • 6
  • You're most probably dereferecing a nullptr leading to undefined behavior. – Anoop Rana May 30 '22 at 07:46
  • I'm quite sure I'm not using the `u` parameter in my code. I was using gdb and every Mat at the point of crash had `u` as nullptr. So I wanted to understand how `u` is being used internally (if at all). – romil May 30 '22 at 08:08
  • You might be implicitly or explicitly dereferencing `u` when it is `nullptr` which leads to undefined behavior according to the given description of the problem in your question. – Anoop Rana May 30 '22 at 09:25
  • 1
    @AnoopRana reopen this question. you don't understand what's happening here. this is an issue with OpenCV or its usage. this is not a generic "dereferencing a null pointer" question, this is specific to code **inside of OpenCV**. when a question is tagged with something other than "C++", be a little more careful in closing it. – Christoph Rackwitz May 30 '22 at 10:16
  • @romil you'll have to provide a [mre]. – Christoph Rackwitz May 30 '22 at 10:16
  • @ChristophRackwitz There is not enough information given in the question to reproduce the problem. If/when the user provides enough information i will consider reopening it. Note that there is no code snippet given by the OP where there is some variable called `u`. The question is incomplete and according to the current description of the problem it seems very likely that the problem is dereferencing a `u` when it is nullptr. We can't say anything for sure until OP provides some code/context. – Anoop Rana May 30 '22 at 10:21
  • yes I see the point, it is unanswerable and should be closed. I disagree with the close reason and I disagree with labeling this as a duplicate. it's not a duplicate. be more careful in the future. – Christoph Rackwitz May 30 '22 at 10:26
  • To clarify, `u` is not a variable. https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a2742469fe595e1b9036f60d752d08461 – romil May 30 '22 at 14:23
  • @AnoopRana I've added more clarification. Please reopen the question. – romil May 30 '22 at 14:45
  • @romil Ok, i have reopened it as now you have added more information and some code snippet showing what you're actually doing. – Anoop Rana May 30 '22 at 15:00

0 Answers0