Skip to content

PhaseLockedVocoderDSP::reset() memory leak #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ void deinit() override {
void reset() override {
SoundpipeDSPBase::reset();
if (!isInitialized) return;
sp_mincer_destroy(&mincer);
sp_mincer_create(&mincer);
sp_mincer_init(sp, mincer, ftbl, 2048);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,66 @@ class PhaseLockedVocoderTests: XCTestCase {

testMD5(audio)
}

func testReset() {
let url = generateTestFile()
XCTAssertNotNil(url)

guard let file = try? AVAudioFile(forReading: url) else {
XCTFail("Couldn't load test file")
return
}

let engine = AudioEngine()
let vocoder = PhaseLockedVocoder(file: file)
engine.output = vocoder

// Start the engine and render some audio
let audio = engine.startTest(totalDuration: 2.0)
vocoder.$position.ramp(to: 0.5, duration: 0.5)
audio.append(engine.render(duration: 1.0))

// Get initial memory usage
var info = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4
var kerr: kern_return_t = withUnsafeMutablePointer(to: &info) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(mach_task_self_,
task_flavor_t(MACH_TASK_BASIC_INFO),
$0,
&count)
}
}
XCTAssertEqual(kerr, KERN_SUCCESS)
let initialMemory = info.resident_size

// Reset the vocoder multiple times to stress test memory management
for _ in 0 ... 1000 {
vocoder.reset()
}

// Get final memory usage
kerr = withUnsafeMutablePointer(to: &info) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(mach_task_self_,
task_flavor_t(MACH_TASK_BASIC_INFO),
$0,
&count)
}
}
XCTAssertEqual(kerr, KERN_SUCCESS)
let finalMemory = info.resident_size

// Calculate memory growth
let memoryGrowth = finalMemory - initialMemory
XCTAssertLessThan(memoryGrowth, 1_000_000, "Memory leak detected: Memory grew by \(memoryGrowth) bytes")

// Continue rendering after reset
vocoder.$position.ramp(to: 0, duration: 0.5)
audio.append(engine.render(duration: 1.0))

engine.stop()

testMD5(audio)
}
}
1 change: 1 addition & 0 deletions Tests/SoundpipeAudioKitTests/ValidatedMD5s.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ let validatedMD5s: [String: String] = [
"-[OscillatorAutomationTests testAutomationAfterDelayedConnection]": "93d96731b7ca3dc9bf1e4209bd0b65ec",
"-[OscillatorAutomationTests testDelayedAutomation]": "640265895a27587289d65a29ce129804",
"-[PhaseLockedVocoderTests testDefault]": "eb9fe2d8ee2e3b3d6527a4e139c2686e",
"-[PhaseLockedVocoderTests testReset]": "814d0574a9d98c76e2dd557050f55f37",
"-[PitchTapTests testBasic]": "5b6ae6252df77df298996a7367a00a9e",
"-[PluckedStringTests testDefault]": "3f13907e6e916b7a4bf6046a4cbf0764",
"-[TalkboxTests testTalkbox]": "316ef6638793f5fb6ec43fae1919ccff",
Expand Down
Loading