Home > Developer Guide > iOS > Testing > KMP GC Crash
KMP GC Crash Investigation
Status: Known Limitation (not a bug in our code)
Impact: Post-test crash during XCTest cleanup - all tests pass successfully
Upstream Issue: KT-68156
Summary
A Kotlin Native memory crash occurs during iOS XCTest execution, manifesting as:
malloc: *** error for object 0x262c5a6f0: pointer being freed was not allocated
Key Finding: This is a known limitation of Kotlin Native with XCTest, not a bug in our code. All 123 tests pass successfully - the crash occurs during test host cleanup/restart.
What Happens
┌─────────────────────────────────────────────────────────────┐
│ 1. XCTest launches app as test host │
│ 2. Kotlin Native framework initializes, creates GC threads │
│ 3. Tests execute successfully (all 123 pass) │
│ 4. XCTest terminates/restarts test host │
│ 5. Kotlin Native GC cleanup triggers double-free ← CRASH │
└─────────────────────────────────────────────────────────────┘
Why It Happens
The crash address (0x262c5a6f0) is deterministic - it’s a static allocation in the Kotlin Native runtime. When XCTest restarts the test host, framework static initializers run again, conflicting with previously allocated memory that wasn’t properly cleaned up.
Test Results
Despite the crash, tests execute correctly:
| Metric | Value |
|---|---|
| Total iOS Tests | 123 |
| Pass Rate | 100% |
| Test Execution Time | ~0.6 seconds |
| Crash Timing | After all tests complete |
Configurations Tested
None of the following configurations resolved the crash:
| Configuration | Result |
|---|---|
kotlin.native.binary.gc=noop | Same crash |
kotlin.native.binary.gc=stwms | Same crash |
kotlin.native.binary.objcDisposeOnMain=false | Same crash |
kotlin.native.binary.gcMarkSingleThreaded=true | Same crash |
Serial test execution (-parallel-testing-enabled NO) | Same crash |
| Clean simulator (erase all content) | Same crash |
| Clear DerivedData | Same crash |
Workarounds
For Xcode IDE Testing
- Continue on debugger break: Press
Cmd+Ctrl+Ywhen debugger stops on the malloc error - Disable malloc breakpoints: In Xcode debugger, don’t set breakpoint on
malloc_error_break - Use command line:
xcodebuild testworks perfectly for automation
For CI/CD
The command-line test runner handles this gracefully:
# xcodebuild returns exit code 0 when all tests pass
xcodebuild -workspace ArcheryApprentice.xcworkspace \
-scheme ArcheryApprentice \
-destination 'platform=iOS Simulator,name=iPhone 16e' \
test
# Check actual test results (ignoring post-test crash)
echo $? # Returns 0 if all tests passedParsing Test Results
For explicit verification in CI scripts:
# Extract test summary
xcodebuild test ... 2>&1 | grep -E "Executed \d+ tests, with \d+ failures"
# Expected output for passing tests:
# Executed 123 tests, with 0 failures (0 unexpected) in 0.612 (0.654) secondsStack Trace Analysis
Typical crash stack trace:
* thread #4, name = 'FinalizerThread main', stop reason = signal SIGABRT
* frame #0: libsystem_kernel.dylib`__pthread_kill
frame #1: libsystem_pthread.dylib`pthread_kill
frame #2: libsystem_c.dylib`abort
frame #3: libsystem_malloc.dylib`malloc_vreport
frame #4: libsystem_malloc.dylib`malloc_zone_error
frame #5: Shared`_ZN6kotlin2gc10FinalizerThreadE...
The crash originates in Kotlin Native’s FinalizerThread, indicating it occurs during garbage collection cleanup, not during test execution.
Impact Assessment
| Aspect | Impact |
|---|---|
| Test Reliability | None - all tests pass |
| CI/CD | None - exit code 0 on success |
| Developer Experience | Minor - debugger breaks on crash |
| Production Code | None - runtime-only issue |
Recommended Approach
- Don’t try to fix it - This is an upstream Kotlin Native issue
- Use command-line testing - More reliable than Xcode IDE for KMP projects
- Monitor upstream - JetBrains is aware of the issue
- Document for team - Ensure developers know this is expected behavior
References
- KT-68156: Kotlin Multiplatform GC crash on iOS
- Kotlin Native Binary Options
- Kotlin/Native Memory Management
Related Documentation
- iOS Test Infrastructure - Complete testing setup guide
- KMP iOS Patterns - Swift-Kotlin interop patterns
Last Updated: 2025-12-06 Investigation completed during iOS XCTest infrastructure documentation