# Debug Compiler Directive in Swift Package causes runtime crash

**URL:** <https://forums.swift.org/t/debug-compiler-directive-in-swift-package-causes-runtime-crash/66701>\
**Category:** Using Swift\
**Tags:** binary-framework, swift-package\
**Created:** [August 10, 2023, 2:46pm UTC](https://forums.swift.org/t/debug-compiler-directive-in-swift-package-causes-runtime-crash/66701 "2023-08-10T14:46:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrtnlst](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mrtnlst/32/31187_2.png) [@mrtnlst](https://forums.swift.org/u/mrtnlst)\
**Post date:** [August 10, 2023, 2:46pm UTC](https://forums.swift.org/t/debug-compiler-directive-in-swift-package-causes-runtime-crash/66701/1 "2023-08-10T14:46:11Z")

</div>

## Summary

I'm facing a crash when using `#if DEBUG` directives in a Swift Package that is linked (but not embedded) to a _release build_ of a static library. If the static library and the Package's library target are then embedded into an App a runtime crash occurs.

If the App target is also build in _release_ or my static library compiles in _debug_, the crash doesn't occur. Similarly, when I remove all `#if DEBUG` directives from the public code in the Swift Package, the App runs fine (in _debug_, whereas the library can stay in _release_).

## Reproduction

I have created a sample project on [GitHub](https://github.com/mrtnlst/SampleProject) that provides a setup to reproduce this issue.

The SampleProject consists of:

- an App Target called **SampleApp**
- a Framework Target called **SampleFramework**
- a local Swift Package called **SamplePackage**

The Dependency Graph looks like this:

```swift
SampleApp
└── embeds SampleFramework
    └── links to SamplePackage
└── embeds SamplePackage

```

If we compile **SampleFramework** in _release_ configuration and add the binary from the build directory to SampleApp, the following crash be witnessed:

 ![Crash-during-Runtime](https://global.discourse-cdn.com/swift/original/3X/a/8/a8d51b753b74efc5c19b023f4155f66b9033acf5.jpeg)

```swift
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0xfffffffffffffff0
Exception Codes: 0x0000000000000001, 0xfffffffffffffff0
VM Region Info: 0xfffffffffffffff0 is not in any region. Bytes after previous region: 18446638520056414193  
      REGION TYPE START - END [VSIZE] PRT/MAX SHRMOD REGION DETAIL
      MALLOC_NANO (reserved) 600018000000-600020000000 [128.0M] rw-/rwx SM=NUL ...(unallocated)
--->  
      UNUSED SPACE AT END
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [50054]

Triggered by Thread: 0

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libswiftCore.dylib 0x18bf8620c _swift_release_dealloc + 16
1 libswiftCore.dylib 0x18bf86df4 bool swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>>::doDecrementSlow<(swift::PerformDeinit)1>(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int) + 128
2 SampleApp 0x100c4de3c outlined destroy of SampleView + 36
3 SampleApp 0x100c4da60 closure #1 in SampleAppApp.body.getter + 108 (SampleApp.swift:15)
4 SwiftUI 0x105cd1b6c 0x1050d4000 + 12573548
5 SampleApp 0x100c4d900 SampleAppApp.body.getter + 144 (SampleApp.swift:14)
6 SampleApp 0x100c4dd98 protocol witness for App.body.getter in conformance SampleAppApp + 12

```

## Questions

This crash raises the following questions for me and I haven't been able to find any answers?

- Should Compiler Directives like `#if DEBUG` not be distributed in the `public` code of a Swift Package and are known to cause crashes when **linked** to binary frameworks?
- What kind of references to a Swift Package dependency are compiled into a static library by Xcode, when the Package is only added via _Target Dependencies_, but **not** to _Frameworks, Libraries and Embedded Content_?

The crash looks to me like the static library, when compiled in _release_ configuration always assumes its dependency (in this case the Swift Package) to be compiled and linked in the same configuration. When the App integrates both, but compiles the Swift Package's code in _debug_ the static library doesn't seem to like that. But this can't be right?
