Page 2 of 2

Re: Debugger permissions?

Posted: Sun Apr 04, 2021 1:51 pm
by mk-soft
I have taken a look at the code.

I noticed that cocoamessage is used in the play(dummy) thread.

In a thread, you have to create a cocoa pool yourself so that it does not conflict with the pool from the MainScope (internal PB). The internal Cocoa Pool is cleaned up when WaitWindowEvent is called. Otherwise this leads to the described error in WaitWindowEvent.

You should also NOT use KillThread, but a variable that tells the thread that it should be terminated.
KillThread only leads to memory leaks ...

Update code Play

Code: Select all

Procedure play(dummy)
  Shared AVAudioPlayer
  Shared nowPlaying
  Protected NSPool.i
  Protected currentTime.d
  
  NSPool = CocoaMessage(0, 0, "NSAutoreleasePool new")
  
  debugLog("playback",nowPlaying\path)
  AVAudioPlayer = CocoaMessage(0,CocoaMessage(0,0,"AVAudioPlayer alloc"),
                               "initWithContentsOfURL:",CocoaMessage(0,0,"NSURL fileURLWithPath:$",@nowPlaying\path),
                               "error:",#Null)
  If AVAudioPlayer
    If CocoaMessage(0,AVAudioPlayer,"play") = #YES
      PostEvent(#evPlayStart)
      While CocoaMessage(0,AVAudioPlayer,"isPlaying") Or nowPlaying\isPaused
        Delay(10)
      Wend
    EndIf
    CocoaMessage(0,AVAudioPlayer,"dealloc")
    AVAudioPlayer = 0
  EndIf
  PostEvent(#evPlayFinish)
  
  CocoaMessage(0, NSPool, "release")
  
EndProcedure

Re: Debugger permissions?

Posted: Sun Apr 04, 2021 4:09 pm
by deseven
mk-soft wrote:In a thread, you have to create a cocoa pool yourself so that it does not conflict with the pool from the MainScope (internal PB). The internal Cocoa Pool is cleaned up when WaitWindowEvent is called. Otherwise this leads to the described error in WaitWindowEvent.
Ah, so that is what this fix was about...
mk-soft wrote:You should also NOT use KillThread, but a variable that tells the thread that it should be terminated.
KillThread only leads to memory leaks ...
Yeah, you're obviously correct, i'm just being lazy.

Thank you so much! I didn't even hope that someone will actually look into it. I'll try again with this new info.

Re: Debugger permissions?

Posted: Wed Apr 07, 2021 8:28 am
by deseven
@mk-soft
I did a massive refactoring based on your comments and i can finally say that everything seems to be stable now. The culprit was the usage of CococaMessage() inside threads and the fix with NSPool works for that just fine.
This means a lot to me, thank you!

P.S. Also got rid of almost all KillThread() calls, although this wasn't the cause for instability.