Fix CallbackSource registrations outliving their sources
Callback-based sound sources are kept alive in a static registry while the C side may still invoke their trampoline, but nothing ever removed them: every callback source leaked its wrapper and closure permanently, and a source retained only by a transient Channel.default wrapper relied on that leak for safety. The registry is now the single source of truth and is purged on Sound.removeSource, Channel.removeSource, and when an owned channel is freed (its sources can no longer be pulled). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,86 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "2700"
|
||||
version = "1.7">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES"
|
||||
buildArchitectures = "Automatic">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "play-date"
|
||||
BuildableName = "play-date"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
shouldAutocreateTestPlan = "YES">
|
||||
<Testables>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "PlayDateTests"
|
||||
BuildableName = "PlayDateTests"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</TestableReference>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "PlayDateTests"
|
||||
BuildableName = "PlayDateTests"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</TestableReference>
|
||||
</Testables>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES"
|
||||
queueDebuggingEnabled = "No">
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "play-date"
|
||||
BuildableName = "play-date"
|
||||
ReferencedContainer = "container:">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
</AnalyzeAction>
|
||||
<ArchiveAction
|
||||
buildConfiguration = "Release"
|
||||
revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
||||
@@ -78,7 +78,9 @@ extension Sound {
|
||||
/// Removes a source from its channel.
|
||||
@discardableResult
|
||||
public static func removeSource(_ source: Source) -> Bool {
|
||||
snd.removeSource.unsafelyUnwrapped(source.pointer) != 0
|
||||
let removed = snd.removeSource.unsafelyUnwrapped(source.pointer) != 0
|
||||
CallbackSource.release(source)
|
||||
return removed
|
||||
}
|
||||
|
||||
/// Sets a callback that records microphone input. Return `false` from the
|
||||
@@ -196,6 +198,11 @@ extension Sound {
|
||||
deinit {
|
||||
if isOwned {
|
||||
Channel.api.freeChannel.unsafelyUnwrapped(pointer)
|
||||
// The freed channel no longer pulls its callback sources, so
|
||||
// their trampoline registrations can be released too.
|
||||
for source in retainedSources where source is CallbackSource {
|
||||
CallbackSource.release(source)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,6 +247,7 @@ extension Sound {
|
||||
public func removeSource(_ source: Source) -> Bool {
|
||||
let removed = Channel.api.removeSource.unsafelyUnwrapped(pointer, source.pointer) != 0
|
||||
retainedSources.removeAll { $0 === source }
|
||||
CallbackSource.release(source)
|
||||
return removed
|
||||
}
|
||||
|
||||
|
||||
@@ -66,10 +66,17 @@ extension Sound {
|
||||
|
||||
let callback: Callback
|
||||
|
||||
/// Sources created through the top-level `Sound.addSource` are kept
|
||||
/// alive here until removed with `Sound.removeSource`.
|
||||
/// Every callback source is kept alive here while the C side may
|
||||
/// still invoke its trampoline: from creation until it is removed
|
||||
/// with `Sound.removeSource`/`Channel.removeSource`, or until its
|
||||
/// owning channel is freed.
|
||||
nonisolated(unsafe) static var live: [CallbackSource] = []
|
||||
|
||||
/// Releases the registration added by `adopt(pointer:)`.
|
||||
static func release(_ source: Source) {
|
||||
live.removeAll { $0 === source }
|
||||
}
|
||||
|
||||
init(callback: @escaping Callback) {
|
||||
self.callback = callback
|
||||
super.init(pointer: nil, isOwned: false)
|
||||
|
||||
Reference in New Issue
Block a user