Commit 13312b6
[ObjCRuntime] Fix GC race in RetainAndAutoreleaseHandle causing intermittent SIGSEGV (#24814)
After obj.GetHandle() extracts the native handle, the GC can finalize obj (a NativeObject, not protected by ObjC runtime reference tracking) before DangerousRetain executes. This causes a use-after-free when the finalizer calls CFRelease on the handle before it's been retained.
Add GC.KeepAlive(obj) to ensure the managed wrapper survives until after the retain+autorelease has completed.
This fixes intermittent SIGSEGV crashes in CGBitmapContext.CreateAdaptive tests (and potentially any other caller of RetainAndAutoreleaseHandle with NativeObject-derived types).
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 9cf7f59 commit 13312b6
File tree
2 files changed
+3
-4
lines changed- src/ObjCRuntime
- tests/cecil-tests
2 files changed
+3
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2337 | 2337 | | |
2338 | 2338 | | |
2339 | 2339 | | |
2340 | | - | |
2341 | | - | |
2342 | | - | |
| 2340 | + | |
| 2341 | + | |
| 2342 | + | |
2343 | 2343 | | |
2344 | 2344 | | |
2345 | 2345 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
152 | | - | |
153 | 152 | | |
154 | 153 | | |
155 | 154 | | |
| |||
0 commit comments