| 481 | |
| 482 | template<class Handler> |
| 483 | void MIDebugSession::addCommandWithCurrentSessionHandler(MI::CommandType type, const QString& arguments, |
| 484 | Handler* handler_this, |
| 485 | MI::MICommand::ResultRecordMethod<Handler> handler_method, |
| 486 | MI::CommandFlags flags) |
| 487 | { |
| 488 | auto cmd = createCommand(type, arguments, flags); |
| 489 | |
| 490 | // Capture the QObject pointer instead of `this` to ensure safety in case the handler is |
| 491 | // invoked from ~MIDebugger(), which is invoked when this->~QObject() destroys child objects. |
| 492 | const QObject* const thisSession = this; |
| 493 | cmd->setHandler(new MI::FunctionCommandHandler( |
| 494 | [guarded_this = QPointer{handler_this}, handler_method, thisSession](const MI::ResultRecord& record) { |
| 495 | auto* const handler = guarded_this.get(); |
| 496 | if (handler && isCurrentDebugSession(thisSession)) { |
| 497 | (handler->*handler_method)(record); |
| 498 | } |
| 499 | }, |
| 500 | flags)); |
| 501 | |
| 502 | queueCmd(std::move(cmd)); |
| 503 | } |
| 504 | |
| 505 | } // end of namespace KDevMI |
| 506 | |