• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Leak while in Midi thread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Leak while in Midi thread


  • Subject: Re: Leak while in Midi thread
  • From: Jeff Moore <email@hidden>
  • Date: Sat, 11 Jan 2003 12:18:27 -0800

The Cocoa documentation has lots of warnings about making AppKit calls from threads other than the main thread (Foundation is marginally better, but not much, you still need autorelease pools for most of those APIs), so you are on shaky ground to begin with.

It all comes down to being cognizant of the environment you are operating in. Threads like those used for MIDI callbacks and for the HAL's IO cycle are more functionally equivalent to hardware interrupts than to normal user threads and really need to be treated as such. Just because you can call malloc() anytime, anywhere doesn't mean that you should.

Instead, you should be pre-allocating all the memory you plan on touching in those threads and making use of things like command queues to convey information from these threads to the rest of your program. Also, you need to avoid doing things that might block the thread (the HAL is especially sensitive to this) like locking a mutex. Further, you have to avoid constructs like spin locks that rely on thread priorities degrading when the thread becomes CPU bound (a spin lock in a time constraint thread is the easiest way to lock your system up).

Hope this helps a bit. You can find out lots more from any good book on real time systems programming.

On Saturday, January 11, 2003, at 01:38 AM, Craig Bakalian wrote:

On Friday, January 10, 2003, at 09:58 PM, email@hidden wrote:

Subject: Re: Leak while in Midi thread
From: Jeff Moore <email@hidden>
To: CoreAudio <email@hidden>

This discussion seems like as good a time as any to remind people that
allocating and deallocating memory inside a time constraint thread such
as this MIDI thread or the HAL's IO thead is flat out bad programming.
The reason why is that the memory calls _will_ block (usually precisely
when you don't want them to) and cause you timing problems that are
very hard to diagnose. This is among the reasons why there are no
autorelease pools in these threads.

To reiterate: don't allocate or deallocate memory in a time constraint
thread owned by the audio or MIDI systems. You will get hurt by doing
so.


So how do we get around this? I am not having timing problems because my app is just getting the tone value or midi value of each midi key press and I am using the numeric key pad to enter the rhythmic value of a given music note. But strange things happen sometimes. Some times the tone value does not change. What do we do when we are using Cocoa?

--

Jeff Moore
Core Audio
Apple
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Leak while in Midi thread
      • From: Lubor Prikryl <email@hidden>
    • Things not to do in a thread (was Re: Leak while in Midi thread)
      • From: Andrew Kimpton <email@hidden>
References: 
 >Re: Leak while in Midi thread (From: Craig Bakalian <email@hidden>)

  • Prev by Date: RE: Leak while in Midi thread
  • Next by Date: Things not to do in a thread (was Re: Leak while in Midi thread)
  • Previous by thread: RE: Leak while in Midi thread
  • Next by thread: Things not to do in a thread (was Re: Leak while in Midi thread)
  • Index(es):
    • Date
    • Thread