Skip to content

Fix MergeDebevec/MergeMertens segfault caused by C++/C# calling convention mismatch - #1878

Merged
shimat merged 5 commits into
mainfrom
claude/fix-photo-hdr-calling-convention
May 26, 2026
Merged

Fix MergeDebevec/MergeMertens segfault caused by C++/C# calling convention mismatch#1878
shimat merged 5 commits into
mainfrom
claude/fix-photo-hdr-calling-convention

Conversation

@shimat

@shimat shimat commented May 26, 2026

Copy link
Copy Markdown
Owner

Summary

Same class of bug as #1876.

MergeDebevec and MergeMertens are affected by the same calling-convention mismatch introduced in #1846: the C# P/Invoke declarations for photo_Ptr_MergeDebevec_get/delete and photo_Ptr_MergeMertens_get/delete
were updated to the new ExceptionStatus + out-parameter convention, but the C++ implementations in photo_HDR.h were left in the old direct-return style.

This causes RawPtr to be populated with garbage/null on construction, leading to a segfault when any method is called on these objects.

Fix

Update the four C++ functions to the convention already used by the surrounding code in the same file (CalibrateDebevec, CalibrateRobertson, etc.): return ExceptionStatus, wrap with BEGIN_WRAP/END_WRAP, and pass the result via an output pointer parameter.

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for HDR exposure-merge operations: native failures are now translated and surfaced as managed exceptions, providing more reliable failure reporting during merge processing.
  • Tests
    • Added tests for MergeDebevec and MergeMertens creation and processing. Tests use synthetic images to validate non-null creations, output format/type and dimensions, and ensure proper resource disposal.

Review Change Stack

…s pattern

Same calling-convention mismatch as #1876: photo_Ptr_MergeDebevec_get/delete
and photo_Ptr_MergeMertens_get/delete were left in the old direct-return style
when #1846 updated the C# P/Invoke declarations to ExceptionStatus + out-param.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@shimat shimat self-assigned this May 26, 2026
These classes had no test coverage, which is why the calling-convention
mismatch introduced in #1846 went undetected until now.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ef9c56f7-5d18-4d9b-8102-c4adde298958

📥 Commits

Reviewing files that changed from the base of the PR and between 939d5fe and 8bc4f33.

📒 Files selected for processing (2)
  • src/OpenCvSharpExtern/photo_HDR.h
  • test/OpenCvSharp.Tests/photo/MergeExposuresTest.cs

📝 Walkthrough

Walkthrough

C API wrapper functions for MergeDebevec and MergeMertens are standardized to return ExceptionStatus with out-parameters, replacing direct pointer/void returns and wrapping operations in exception-handling macros. Comprehensive unit tests for both merge algorithms verify successful creation, exercise Process overloads with test data, and validate output properties.

Changes

Photo HDR API Exception Safety

Layer / File(s) Summary
Exception-safe merge pointer helpers
src/OpenCvSharpExtern/photo_HDR.h
photo_Ptr_MergeDebevec_delete, photo_Ptr_MergeDebevec_get, photo_Ptr_MergeMertens_delete, and photo_Ptr_MergeMertens_get are updated to return ExceptionStatus and use BEGIN_WRAP/END_WRAP macros; get functions write results into out-parameters instead of returning pointers directly.
Merge exposure test coverage
test/OpenCvSharp.Tests/photo/MergeExposuresTest.cs
New test class with helper method to generate three 100x100 exposure images; test methods validate factory creation, run Process with appropriate parameters for each merge algorithm, assert output type (CV_32FC3) and dimensions, and ensure proper resource cleanup.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

bug

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: fixing a segfault in MergeDebevec/MergeMertens caused by a C++/C# calling convention mismatch, which aligns with the primary objective and code changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/fix-photo-hdr-calling-convention

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai

coderabbitai Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8fdc40bd-ccd2-4c0c-a329-9c0ce20dcc6b

📥 Commits

Reviewing files that changed from the base of the PR and between 8b04eec and 9d5cede.

📒 Files selected for processing (1)
  • test/OpenCvSharp.Tests/photo/MergeExposuresTest.cs

📝 Walkthrough

Walkthrough

C wrappers for MergeDebevec/MergeMertens and their process functions now return ExceptionStatus and use out-parameters; P/Invoke signatures are updated, managed callers wrap native calls with centralized exception handling, and xUnit tests for both merge algorithms were added.

Changes

Photo HDR API Exception Safety

Layer / File(s) Summary
C wrapper: ExceptionStatus + out-params
src/OpenCvSharpExtern/photo_HDR.h
Convert photo_createMergeDebevec/photo_createMergeMertens, photo_Ptr_*_get/delete, and photo_..._process to return ExceptionStatus, use out-parameters for pointer returns, and wrap bodies with BEGIN_WRAP/END_WRAP.
P/Invoke signatures: return ExceptionStatus
src/OpenCvSharp/Internal/PInvoke/NativeMethods/photo/NativeMethods_photo_HDR.cs
Update P/Invoke return types for photo_MergeExposures_process and photo_MergeMertens_process to ExceptionStatus and remove an obsolete comment.
Managed callers wrap native calls
src/OpenCvSharp/Modules/photo/MergeExposures.cs, src/OpenCvSharp/Modules/photo/MergeMertens.cs
Call sites now invoke native methods via NativeMethods.HandleException(...) to translate ExceptionStatus into managed exceptions.
Unit tests: Merge exposures and mertens
test/OpenCvSharp.Tests/photo/MergeExposuresTest.cs
Add MergeExposuresTest with helper CreateExposureImages() and four tests verifying Create() and Process() for both MergeDebevec and MergeMertens (output type, non-empty, dimensions).

Sequence Diagram(s):

sequenceDiagram
  participant MergeExposuresProc as MergeExposures.Process (managed)
  participant MergeMertensProc as MergeMertens.Process (managed)
  participant NativeMethods as NativeMethods (P/Invoke)
  participant ExternWrappers as photo_HDR (C wrappers)
  participant OpenCV as cv::Merge* (C++)

  MergeExposuresProc->>NativeMethods: HandleException(photo_MergeExposures_process(...))
  MergeMertensProc->>NativeMethods: HandleException(photo_MergeMertens_process(...))
  NativeMethods->>ExternWrappers: call exported function returning ExceptionStatus
  ExternWrappers->>OpenCV: obj->process(...) / create/get/delete (BEGIN_WRAP/END_WRAP)
  ExternWrappers-->>NativeMethods: ExceptionStatus (ok or error)
  NativeMethods-->>MergeExposuresProc: translate via HandleException (throw on error)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested labels

bug

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: addressing a C++/C# calling convention mismatch causing segfaults in MergeDebevec and MergeMertens classes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/fix-photo-hdr-calling-convention

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

shimat and others added 2 commits May 26, 2026 22:38
…y ExceptionStatus pattern

- photo_createMergeDebevec/MergeMertens were still returning pointer directly
  while C# side expected ExceptionStatus + out param, causing null ptr crash
- photo_MergeExposures_process and photo_MergeMertens_process: void→ExceptionStatus
- Wrap all call sites with NativeMethods.HandleException

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Callers dispose the returned Mats in finally blocks; the warning is a false positive.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/OpenCvSharp/Modules/photo/MergeMertens.cs (1)

1-1: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Save this file with a UTF-8 BOM.

This file appears to be committed without the BOM required for .cs sources in this repo.

As per coding guidelines, **/*.{cs,csproj,yml,md,json}: All source files must be saved as UTF-8 with BOM (EF BB BF)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/OpenCvSharp/Modules/photo/MergeMertens.cs` at line 1, The file
MergeMertens.cs (contains using OpenCvSharp.Internal and the MergeMertens class)
must be saved with a UTF-8 BOM; reopen the file in your editor or commit tool
and resave/encode it as UTF-8 with BOM (EF BB BF) so it conforms to the
repository's encoding rule for .cs files, then recommit the updated file.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/OpenCvSharp.Tests/photo/MergeExposuresTest.cs`:
- Line 1: The file MergeExposuresTest.cs is missing the required UTF-8 BOM;
reopen the file and re-save it with UTF-8 with BOM (EF BB BF) encoding so it
complies with the repo guideline for C# sources (ensure your editor or git
config writes the BOM), then commit the updated file (referenced symbol: the
test file MergeExposuresTest.cs).

---

Outside diff comments:
In `@src/OpenCvSharp/Modules/photo/MergeMertens.cs`:
- Line 1: The file MergeMertens.cs (contains using OpenCvSharp.Internal and the
MergeMertens class) must be saved with a UTF-8 BOM; reopen the file in your
editor or commit tool and resave/encode it as UTF-8 with BOM (EF BB BF) so it
conforms to the repository's encoding rule for .cs files, then recommit the
updated file.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d4ebaa05-54b7-4b8f-a47f-870951f44442

📥 Commits

Reviewing files that changed from the base of the PR and between 672e129 and 8b04eec.

📒 Files selected for processing (5)
  • src/OpenCvSharp/Internal/PInvoke/NativeMethods/photo/NativeMethods_photo_HDR.cs
  • src/OpenCvSharp/Modules/photo/MergeExposures.cs
  • src/OpenCvSharp/Modules/photo/MergeMertens.cs
  • src/OpenCvSharpExtern/photo_HDR.h
  • test/OpenCvSharp.Tests/photo/MergeExposuresTest.cs

Comment thread test/OpenCvSharp.Tests/photo/MergeExposuresTest.cs Outdated
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@shimat
shimat merged commit 55b224f into main May 26, 2026
7 of 8 checks passed
@shimat
shimat deleted the claude/fix-photo-hdr-calling-convention branch May 26, 2026 13:58
@shimat shimat added the bug Confirmed defect in OpenCvSharp label Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Confirmed defect in OpenCvSharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant