dmrschmidt / DSWaveformImage

Generate waveform images from audio files on iOS, macOS & visionOS in Swift. Native SwiftUI & UIKit views.
MIT License
1.04k stars 113 forks source link

Memory issue #28

Closed MiriWeinstein closed 2 years ago

MiriWeinstein commented 2 years ago

Loading url to waveformImageView -> popping out to previous vc before waveformImageView finished loading -> crashes on memory issue. Is there an option to stop loading the waveformImageView? or kill the object?

dmrschmidt commented 2 years ago

Thanks for reporting this @MiriWeinstein!

Would you be able to support me with a stack trace of the crash here so I can narrow down where exactly the issue may be?

if you could provide a bit of the code in the context where the library is used, that would also be extremely helpful.

Appreciate your help :)

MiriWeinstein commented 2 years ago

The function I'm using:

func initWaveformImageView(url: URL) {
    waveformImageView  = WaveformImageView(frame:  CGRect(x: UIScreen.main.bounds.width/2, y: 0, width: 2000, height: 40))
    scrollView.addSubview(waveformImageView!)
    scrollView.contentSize = CGSize(width: waveformImageView!.frame.width + UIScreen.main.bounds.width , height: 40)

    waveformImageView?.waveformColor = UIColor(red: 51/255.0, green: 92/255.0, blue: 103/255.0, alpha: 1)
    waveformImageView?.waveformStyle = .striped
    waveformImageView?.waveformAudioURL = url

}

error: Failure Reason: Message from debugger: Terminated due to memory issue User Info: { IDERunOperationFailingWorker = DBGLLDBLauncher; }

dmrschmidt commented 2 years ago

Hmm, is that the only output? I’d ideally need a stack trace to see where exactly the problem occurred.

With this information I can only go with a very vague hunch right now: is the size of the audio file very large? Like more than 100MB or so possibly? That might cause memory issues potentially. It’s not specifically optimized for huge files.

Unfortunately, I will be hiking through the dense jungle of Thailand’s backcountry from today onwards and will be off the grid for probably around a week. So I won’t be able to get back in a timely manner. I hope the hunch with the size might be pointing into a working direction.

MiriWeinstein commented 2 years ago

Thanks for responding, the file size is 15MB, the memory issue crash happens when I open the view controller that tries to draw the wave, and then, before it's completed the drawing, the user popped out to previous VC, then opening again then popping again, than memory issue (with no log, just popup in xcode) The question is - how can I deinit the image or stop the loading?

dmrschmidt commented 2 years ago

Ah alright, sorry you yes you had mentioned that. I misunderstood, my bad. When I’m back in civilization I’ll have a look and try to reproduce the issue. Really sorry for the longer delay until I can get to it. On 13. Feb 2022, 19:48 +0700, MiriWeinstein @.***>, wrote:

Thanks for responding, the file size is 15MB, the memory issue crash happens when I open the view controller that tries to draw the wave, and then, before it's completed the drawing, the user popped out to previous VC, then opening again then popping again, than memory issue (with no log, just popup in xcode) — Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.Message ID: @.***>

dmrschmidt commented 2 years ago

With much of a delay now... Is this issue still happening to you? I had tried reproducing it but with no luck.

From my tests with the memory profiler it all looked pretty alright to me in my scenarios. I know this has been a long time ago now and you probably moved on. But if you can still reproduce this, I'm wondering if we could maybe to a remote pairing session to look at this. Let me know.

MiriWeinstein commented 2 years ago

Yes, it still happening, not on first time, but if you open, close, open, close a few times before the wave finishes loading, it crashes on memory issue. I chose a large file (15 mb) so it takes time for the wave to draw itself

dmrschmidt commented 2 years ago

OK that's good to hear. I mean that it's still reproducible for you. Not that it's still happening at all ;)

As I mentioned, I did not manage to reproduce this locally, so I'd be very keen to investigate this further.

If this was possible at all for you and you'd be open to looking into this, I'd love to do a 15-20 minute zoom screen sharing session maybe? That would probably not be enough to fix the issue but it should give me enough insight to make this reproducible for myself. Is this something you'd generally be open to? I acknowledge that your time is precious as well of course, so I'd try to keep it short.

Alternatively, if you could share the code in context, that might also be enough already. I guess I'd be need to see the ViewController it is in, as well as the way the navigation is performed during this open / close cycle.

MiriWeinstein commented 2 years ago

Unfortunately, a zoom meeting is not possible for me, but I will send you the code on Sunday.

dmrschmidt commented 2 years ago

No rush from my side :) That would be very much appreciated if you could share a bit more of it. Thanks so much :)

MiriWeinstein commented 2 years ago

WaveAudioView.pdf

dmrschmidt commented 2 years ago

Hey @MiriWeinstein,

thanks for sending this snippet. So far, I still wasn't able to reproduce this.

I am beginning to suspect, that there possibly may be a bug in your navigation setup and possibly not within DSWaveformImage.

In practice, when you dismiss the WaveAudioView, which must sit in some UIViewController, the UINavigationController that holds those, is responsible for releasing the UIViewController, which would then release all its "child memory". In my own replicated example, this works also when I tap back and forth quickly.

In the code you shared, I cannot see the navigation logic unfortunately, so this is all speculation still, of course. But what may happen, is that somewhere in that logic, something keeps retaining the view controller that is being pushed onto the navigation stack and thus prevents it from being released when it is being popped again.

What that might be is impossible to tell without seeing more of the code.

What I would recommend is investigating this further with the Profiler in Instruments. You can go there by going into the "Memory Profiler" (see image below). It has a button on the top right labeled "Profile in Instruments"

Screenshot 2022-04-04 at 08 34 30

(here you can see these little spikes from me going back and forth to and back from that view. What you will see there - based on your problem description - is that the memory keeps increasing with every push of that view and never go back. Can you confirm that this is what you see?)

If you have "Profile in Instruments" open (that button is cut from the above screenshot, but it is top right), you will be able to go to this view here and then drill down into the memory consumption call tree. In there you should look for a line like in this screenshot that contains WaveformAnalyzer.waveformSamples. Then the lines around it, so "up the tree" should give you an indication where those are coming from and why they don't get released.

Screenshot 2022-04-04 at 08 45 46

Let me know if this helps. I'd be very curious to hear your findings. I definitely don't wanna rule out a problem within the library, but right now I'm expecting it may be in your navigation logic.

dmrschmidt commented 2 years ago

closing since no further activity