Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!hellgate.utah.edu!helios.ee.lbl.gov!ux1.lbl.gov!beard From: beard@ux1.lbl.gov (Patrick C Beard) Newsgroups: comp.sys.mac.programmer Subject: Re: SndNewChannel crashes my program! Keywords: c, Sound, snd, code Message-ID: <4846@helios.ee.lbl.gov> X-Local-Date: 14 Feb 90 02:03:40 PST Date: 14 Feb 90 10:03:40 GMT References: <7605@tank.uchicago.edu> Sender: usenet@helios.ee.lbl.gov Reply-To: beard@ux1.lbl.gov (Patrick C Beard) Distribution: usa Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 47 In article <7605@tank.uchicago.edu> fri0@tank.uchicago.edu (Christian E. Fritze) writes: # #Dear Netters! # #I'm a beginning Mac programmer trying to use the sound manager routines to play #some sampled sound. Why does the following code (an excerpt from my THINK C #4.0 program) break my Mac at the indicated function call? Odd address, says #the debugger; sound doesn't play. IMV says that passing a NIL pointer to #SndNewChannel tells SndNewChannel to allocate a channel for you and return it. ##include ##define NULL 0L # #main() # #{ # #SndChannelPtr soundPtr; #Handle soundHndl; #OsErr Check; # #soundHndl = GetResource('snd ',16658); # # /*This call does it... */ #Check=SndNewChannel(&soundPtr,sampledSynth,initSRate22k,NULL); As you said, IMV says that passing a NIL pointer gets you a new channel. You forgot to initialize the variable soundPtr to nil! Therefore, you get a crash when SndNewChannel gets whatever happens to be on the stack. I won't comment on the "stupidity" of this question. Apple, why did you come up with such a bizarre way to allocate a channel? I much prefer the NewWindow method. In other words, we can preallocate memory for Windows and pass it to NewWindow: WindowPtr myWindow = (WindowPtr)NewPtr(sizeof(WindowRecord)); WindowPtr w = NewWindow(myWindow, bounds, title, visible, procID, behind, goAwayFlag, refCon); I suppose the rationale was that an OSErr code should be returned because opening a channel does more than allocate memory for the channel record, but perhaps a SoundError() function would be better. ------------------------------------------------------------------------------- - Patrick Beard, Macintosh Programmer (beard@lbl.gov) - - Berkeley Systems, Inc. "..............Good day!" - Paul Harvey - -------------------------------------------------------------------------------