J'ai ce morceau de code ici:Segmentation fault lors de la capture des exceptions dans une application liée à libpthread (linux, C++)
Ces fonctions sont utilisées pour créer et arrêter un pthread:
void WatchdogController::conscious_process_handler_start() {
if (debug) cout << "WatchdogController: starting conscious process thread" << endl;
cn_pr_thread_active = true;
if (pthread_create(&cn_pr_thread, NULL, conscious_process_handler, this) < 0) {
cn_pr_thread_active = false;
throw WatchdogException("Unable to start new thread");
}
}
void WatchdogController::conscious_process_handler_stop() {
if (debug) cout << "WatchdogController: stopping conscious process thread" << endl;
cn_pr_thread_active = false;
int *retval;
pthread_join(cn_pr_thread, (void **)&retval);
if (*retval < 0) {
delete retval;
string err = string("Error returned by conscious_process_handler(): ") + string(pthread_err);
throw WatchdogException(err.c_str());
}
delete retval;
}
J'utilise select() dans la fonction transmise à pthread, et lorsqu'il est stoppé, il renvoie une erreur résultant de la valeur négative de pthread, mais ce n'est pas le problème, je le corrigerai plus tard - le problème est que lorsque l'exception est lancée ici:
throw WatchdogException(err.c_str());
et pris ici:
try {
watchdog_controller->hardware_watchdog_stop();
watchdog_controller->unconscious_process_handler_stop();
watchdog_controller->conscious_process_handler_stop();
}
catch (HardwareWatchdogException &e) {
cerr << "Error stopping hardware watchdog!" << endl;
cerr << e.get_reason() << endl;
string err = string("Exception thrown by hardware watchdog controller") + string(e.get_reason());
if (log) write_log(err.c_str());
delete watchdog_controller;
return -1;
}
catch (WatchdogException &e) {
cerr << "Exception cought when exiting!" << endl;
cerr << e.get_reason() << endl;
string err = string("Exception cought when exiting") + string(e.get_reason());
if (log) write_log(err.c_str());
delete watchdog_controller;
return -1;
}
je reçois une erreur de segmentation puis essayez d'accéder à l'objet à ce stade:
cerr << e.get_reason() << endl;
Quelle pourrait être la raison?
Référence & e indique quelque chose, mais il semble que l'adresse était invalide.
est ici la classe d'exception:
class WatchdogException {
public:
/**
@brief Default constructor
*/
WatchdogException() : reason() {
}
/**
@brief Overloaded constructor - setting the error message
@param why Error message
*/
WatchdogException(const char *why) : reason(why) {
}
/**
@brief The destructor
*/
virtual ~WatchdogException() {
}
/**
@brief A getter for the error message
@return Returns a string containing error description
*/
virtual std::string get_reason() const {
return reason;
}
protected:
/**
@var reason String containing the error message
*/
std::string reason;
};
cela fait-il une différence si vous l'avez attrapé par const ref? – Naveen
Rien ne me semble aussi faux ici. Pouvez-vous obtenir un backtrace dans gdb? –
Lorsqu'il est exécuté sous gdb, il se bloque sur pthread_join - Je pense que cela a quelque chose à voir avec gdb ne passant pas les signaux correctement - comment configurer gdb pour passer tous les signaux à programmer? – zbigh