Repository navigation
Unify native→managed error propagation; drop the managed error callback - #1969
Merged
Merged
Conversation
Phase 3 of the internal-layer modernization (#1966, part of #1938). Replaces the split error mechanism (Windows: throw a managed exception from the OpenCV error callback through native frames; *nix: record into a managed ThreadLocal via the callback) with a single portable path on all platforms: * cvTry now wraps every body in try/catch and records the caught exception's details (code/func/file/line/message) into a per-thread native store, captured directly from the thrown object. catch(...) ensures any C++ exception is reported rather than crossing the boundary as SEHException; only non-C++ faults (SEH/SIGSEGV) pass through. * New exports: core_getLastException (read the per-thread record) and core_setSilentErrorHandler (install a native, managed-free no-op handler that merely mutes OpenCV's stderr dump). * Managed: ExceptionHandler builds the OpenCVException from the native record; no CvErrorCallback delegate is installed in the default path (NativeAOT/trim friendly). HandleException simply throws on Occurred; the now-dead DOTNETCORE conditionals are removed. * Cv2.SetErrorHandler(CvErrorCallback?) is the opt-in escape hatch; null restores the native silent handler. Because cvTry catches on the calling (P/Invoke) thread, error details are correct even when cv::parallel_for_ rethrows a worker exception there -- the old callback-on-worker-thread + ThreadLocal scheme was wrong in that case. Requires rebuilding OpenCvSharpExtern; native build verified on CI. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add smoke tests for the new opt-in managed error handler: - a custom handler is invoked on a native error while the error still surfaces as an OpenCVException (with the expected status/func/message); - SetErrorHandler(null) restores the default native silent handler and errors still surface as OpenCVException. The existing ExceptionTest cases already cover the default getter-based path (native exception -> OpenCVException with code/func/file/line/message). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This was referenced Jun 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@
Layer B of Phase 3 — closes #1966 (part of #1938). Stacked on #1968 (review/merge that first; this PRs diff is only the mechanism change).
Replaces the split error mechanism — Windows: throw a managed exception from the OpenCV error callback through native frames; *nix: record into a managed
ThreadLocalvia the callback — with a single portable path on all platforms.Design
cvTrynow wraps each body in a three-tiercatch(cv::Exception→std::exception→...) and records the caught exceptions details into a per-thread native store, captured directly from the thrown object.catch(...)guarantees any C++ exception is reported rather than crossing the P/Invoke boundary asSEHException; only non-C++ faults (SEH /SIGSEGV) pass through.core_getLastException(read the per-thread record) andcore_setSilentErrorHandler(install a native, managed-free no-op handler that merely mutes OpenCVs stderr dump).ExceptionHandlerbuildsOpenCVExceptionfrom the native record; noCvErrorCallbackdelegate is installed in the default path (NativeAOT / trim friendly, removes a delegate from the Redesign legacy DisposableObject-based utilities (ArrayAddress1/2, Window, CvTrackbar, ResourcesTracker) #1941 set).HandleExceptionsimply throws onOccurred; the now-deadDOTNETCOREconditionals are removed.Cv2.SetErrorHandler(CvErrorCallback?)— opt-in managed handler;nullrestores the native silent handler.Why its more correct
cvTrycatches on the calling (P/Invoke) thread, so details are right even whencv::parallel_for_rethrows a worker exception there. The old callback-on-worker-thread +ThreadLocalscheme recorded on the wrong thread in that case.Notes
cv::utils::logging) is tracked independently in [5.x] Expose OpenCV's logging system (cv::utils::logging) through a managed wrapper #1967.Requires rebuilding
OpenCvSharpExtern; native build + runtime behavior verified on CI.@