EXEC_BAD_ADDRESS
maybe this is the continuation of this thread,
The program compiles without errors or warnin开发者_如何学Cgs but when I run it, and the handler function is called, I get EXEC_BAD_ADDRESS
void MainController::show_color_trackbars(int *h, int *s, int *v){
String winName = "HSV Trackbars";
namedWindow(winName, CV_WINDOW_AUTOSIZE);
std::map<String, void*> user_data_h;
user_data_h["Object"] = this; //this is a MainController object
user_data_h["h"] = h;
createTrackbar("trackbar_H", winName, h, 255, trackbar_handler, &user_data_h);
};
void trackbar_handler(int value, void *user_data){//callback for the track bar
std::map <String, void*> *user_data_map;
user_data_map = reinterpret_cast<std::map<String, void *> *>(user_data);
MainController *controller;
controller = reinterpret_cast<MainController *>((*user_data_map)["Object"]);
int *var;
var = reinterpret_cast<int*> ((*user_data_map)["h"]);
//do something with controller and var
};
I am mistaking something when casting? I cannot think of another reason this code is failing.
Thanks in advance
That's because in all probablity user_data_h
is a local variable and is already destroyed when trackbar_handler
is called. trackbar_handler
works on a pointer which is no longer valid!
Please check if it is okay to have user_data_h
dynamically allocated and register that pointer with the callback dispatch.
精彩评论