Opencv trackbar crash when I slide

Hi everyone,
I created a configuration window with createTrackbar to set up my variables for the inRange() function and others. The trackbar slide is faster for this. So I created several createTrackbar in the same configuration window.
When I build, I have no error. But when the application is launched and I move my cursor on some trackbars my application crashes and is done automatically. I have the following error message: “segmentation fault (core dumped)”.

can you show your code, please ?
also, opencv version ? what did you install, and how ?
are you able to debug it (get a stacktrace) ?

I can’t show all the code but, to give an exemple of what I wrote and where is the problem locate.

 {
    int lowHue =0
    int lowSaturation = 0
    int lowValue = 0
    int highHue = 255;


     cv::namedWindow("Configuration");

	cv::createTrackbar("Low hue","Configuration", &lowHue, 255, OnChangeLowHue, this);
	cv::createTrackbar("Low saturation","Configuration", &lowSaturation, 255, OnChangeLowSaturation, this);
	cv::createTrackbar("Low Value", "Configuration", &lowValue, 255, OnChangeLowValue, this);
	cv::createTrackbar("High hue", "Configuration", &highHue, 255, OnChangeHighHue, this);

}

static void Window::OnChangeLowHue(int lowHue, void * thisWindow)
{
    Window* that = (Window*) thisWindow;
    that->Foo.LowHue(lowHue);
}
static void Window::OnChangeHighHue(int highHue, void * thisWindow)
{
    Window * that = (Window*) thisWindow;
    that->Foo.HighHue(highHue);
}

......

The opencv version is 4.2 and I install on Ubuntu 20.4 with de package : sudo apt install libopencv-dev

No I don’t debug it.

that’s terribly old

hmm, those are local variables (inside the constructor) ?
if so, the address you pass to createTrackbar()
will be invalid later (“dangling pointer”)
once you move the trackbar, it will try to write the current value back to this address, aand it goes kaboom.

rather use a pattern like:

cv::createTrackbar("Low hue","Configuration", NULL, 255, OnChangeLowHue, this);
cv::setTrackbarPos("Low hue","Configuration", lowHue);
1 Like

ok for the version, I’ll update later.
Yes, I use local variables. I said that it could come from variables that don’t point to the same place anymore because before putting in a constructor I was using these variables as global variables. But I didn’t see how to do otherwise, I’ll try what you suggest and come back to you.

Edit : You are write, it’s because there is “dangling pointer”. It works perfectly. Thank you very much for you help and quick answers.

1 Like