CLBackgroundActivitySession Crash Issue - Misleading Apple Guidance

I encountered a crash in iOS 17 related to CLBackgroundActivitySession, which appears to be due to misleading guidance in an Apple’s WWDC video.

Crash sample code: https://github.com/steve-ham/AppleLocationCrash

Simplified Reproduction Steps:

1.	Open the GitHub sample app.
2.	Archive and export (Distribute App -> Custom -> (Release Testing, Enterprise, or Debugging) -> Export).
3.	Open the app.
4.	Tap enableBackgroundLocation -> select Allow While Using App on the system popup.
5.	Tap disableBackgroundLocation.
6.	Go to the iOS home screen.
7.	Wait for 10 seconds.
8.	Reopen the app -> crash occurs.

The crash happens because setting CLBackgroundActivitySession to nil does not end the session, despite Apple’s guidance suggesting it should. Below is the exact quote from WWDC 2023, which explicitly states that both calling invalidate() or letting the object get destroyed (i.e., setting to nil) would end the session:

WWDC 2023 Discover Streamlined Location Updates (https://developer.apple.com/videos/play/wwdc2023/10180/)

“Before starting the updates, you should instantiate a CLBackgroundActivitySession object to start a new session. Note, we are assigning the session to self.backgroundActivity, which is a property and not to a local variable. And this is important because if we used a local variable, then when it goes out of scope, the object it holds would be deallocated, invalidating the session and potentially ending your app’s access to location. Then when we want to end our session, we can do that by sending the invalidate message or by letting the object be destroyed.”

I’ve submitted this to Apple for resolution but wanted to share this with the community. This misguidance has caused issues in my app’s release. If Apple could reply to confirm or provide clarification, it would be greatly appreciated.

P.S. Even a minimal implementation in viewDidLoad triggers the crash:

let session = CLBackgroundActivitySession() print("session (session)")

First we need to understand what the cause of the crash is. If you’re setting an object variable to nil and then accessing it, that would be an obvious cause.

So start with Acquiring crash reports and diagnostic logs to create a symbolicated crash log so we can see what’s going on.

You can attach the crash log here, and/or send it to the support request you have also initiated, and we’ll take a look.

or

https://github.com/steve-ham/AppleLocationCrash/blob/main/AppleLocationCrash/AppleLocationCrashLog.crash

Thanks for answering. I'm not accessing the variable after setting it to nil.

Here’s the log: When CLBackgroundActivitySession deallocates in any form without an explicit call to invalidate, the crash occurs after reproducing the steps following step 5. The documentation does not mention this, and WWDC guidance suggests calling invalidate OR destroy to end the session (but destroy causes a crash):

The crash originates from Apple’s thread and ChatGPT thinks it's memory management issue for Apple's Core Location team:

Thread 1 name:   Dispatch queue: com.apple.corelocation.shared
Thread 1 Crashed:
0   libobjc.A.dylib               	       0x186c2e870 objc_release_x8 + 16
1   libsystem_blocks.dylib        	       0x211e6d860 bool HelperBase<ExtendedInline>::disposeCapture<(HelperBase<ExtendedInline>::BlockCaptureKind)3>(unsigned int, unsigned char*) + 67
2   libsystem_blocks.dylib        	       0x211e6d570 HelperBase<GenericInline>::destroyBlock(Block_layout*, bool, unsigned char*) + 159
3   libsystem_blocks.dylib        	       0x211e6d030 _call_dispose_helpers_excp + 71
4   libsystem_blocks.dylib        	       0x211e6cfcc _Block_release + 255
5   libsystem_blocks.dylib        	       0x211e6ce90 bool HelperBase<ExtendedInline>::disposeCapture<(HelperBase<ExtendedInline>::BlockCaptureKind)4>(unsigned int, unsigned char*) + 67
6   libsystem_blocks.dylib        	       0x211e6d584 HelperBase<GenericInline>::destroyBlock(Block_layout*, bool, unsigned char*) + 179
7   libsystem_blocks.dylib        	       0x211e6d030 _call_dispose_helpers_excp + 71
8   libsystem_blocks.dylib        	       0x211e6cfcc _Block_release + 255
9   libdispatch.dylib             	       0x1916460d0 _dispatch_client_callout + 19
10  libdispatch.dylib             	       0x19164d6d8 _dispatch_lane_serial_drain + 743
11  libdispatch.dylib             	       0x19164e1e0 _dispatch_lane_invoke + 379
12  libdispatch.dylib             	       0x191659258 _dispatch_root_queue_drain_deferred_wlh + 287
13  libdispatch.dylib             	       0x191658aa4 _dispatch_workloop_worker_thread + 539
14  libsystem_pthread.dylib       	       0x211e73c7c _pthread_wqthread + 287
15  libsystem_pthread.dylib       	       0x211e70488 start_wqthread + 7
CLBackgroundActivitySession Crash Issue - Misleading Apple Guidance
 
 
Q