Getting Compilation segmentation fault (core dumped) when running a C wrapper for OpenCV's C++ createTrackbar

StackOverflow https://stackoverflow.com/questions/22675530

  •  22-06-2023
  •  | 
  •  

Frage

Here is my wrapper for createTrackbar. I'm not sure if it's the way I'm using it or the way it's written. So far I changed the O's in onChange and userdata to NULL's, I changed the String* types to const char*. It could be the way I defined TrackbarCallback* as a pointer. but I'm not sure of any other way to define it.

int cv_createTrackbar(String*  trackbarname,String* winname, int* value, 
    int count, TrackbarCallback* onChange=0, void* userdata=0)
{
    return cv::createTrackbar(*trackbarname, *winname, value,
        count, *onChange, userdata);
}

Here is a section of the code I used it in. all of the other code ran but when I added just the cv_createTrackbar line I got the error in the post title. I take out the line and the code runs fine. I tried calling the new String (a) and (b) as *(&a) when I declared a and b as const char* I also tried calling as &a and &b right after. All my old tricks don't seem to be working on this one so if someone can give me advice on this I sure would appreciate it.

 namedWindow("My Window", 1);
 const char* a = "Contrast";
 const char* b = "My Window";
 int iSliderValue1 = 50;
 cv_createTrackbar(new String (a), new String(b), &iSliderValue1, 100);

Error Again: Compilation segmentation fault (core dumped) at Wed Mar 26 16:35:55

War es hilfreich?

Lösung

The onChange argument has a default value of 0 (NULL). You do not pass onChange to cv_createTrackbar so the default value is used. You attempt to dereference it within your function as *onChange. Dereferencing a NULL pointer will result in a segmentation fault (core dumped).

The prototype for createTrackbar is:

int createTrackbar(const string& trackbarname, const string& winname,
    int* value, int count, TrackbarCallback onChange=0, void* userdata=0);

And the documentation states:

onChange – Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.

Since onChange is expecting a pointer and the documentation specifically states that NULL is valid, you need to change *onChange to onChange. The dereference is not necessary.

Also, change TrackbarCallback* to TrackbarCallback for your function arguments. It doesn't need to be a pointer to a pointer.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top