Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!caen.engin.umich.edu!netnews From: netnews@caen.engin.umich.edu (netnews) Newsgroups: comp.sys.mac.programmer Subject: Re: Config File Resource Blues Message-ID: <444accf7.a590@mag.engin.umich.edu> Date: 8 Jul 89 07:11:00 GMT References: <2002@dogie.macc.wisc.edu> Reply-To: mystone@sol.engin.umich.edu Organization: Computer Aided Engineering Network, University of Michigan Lines: 86 Sender: Followup-To: In article <2002@dogie.macc.wisc.edu> yahnke@vms.macc.wisc.edu (Ross Yahnke, MACC) writes: >I'm having problems maintaining a configuration file for my >application. It lives in the System Folder and is a file w/a >resource fork containing some data that gets read in when I >open a certain type of document window in my app, and then gets >written out when I close the window. > >My problem is when I re-open the window in the same session, >and try to reread the resource, the GetResource call goofs up. > >Below are two code fragments, ReadConfig() gets called when I >open my document, the first time it's called it works fine, >GetResource returns a handle that points to my data, and >dataLen is correct. Then when I close the document I call >WriteConfig. The data that I'm maintaining may have changed and >the config file data would therefore be out of date, so I >automatically clear it, create a new handle, stream my data >into the handle, and add the resource. If I quit my app at this >point and examine my config file w/ResEdit, the data looks >hunky-dory. But if I instead try to reopen my document window, >thereby calling ReadConfig again, the GetResource mysteriously >returns some bogus handle (I think) that points to nothing >significant, and dataLen is bad as well. > >ResError() never returns a bad value, to the best of my >knowledge. The value of CurResFile() never changes when I check >it a various locations around this code. The config file is >opened at the program start by a call to OpenResFile, which is >set to look in the System Folder. I'm not doing anything >significant, I guess, between opening, closing, and then >opening my document. > >Any ideas/comments? > >ReadConfig() >{ Handle dataHdl; > long dataLen; > > dataHdl = GetResource('DATA', 128); > dataLen = SizeResource(dataHdl); > > /* At this point I stream the data into > * a application maintained structure. This > * data may change as the user interacts > * w/the app. > */ >} > >WriteConfig() >{ Handle dataHdl; > Handle aHdl; > > /* Zap the old data as it is most likely out of date */ > dataHdl = GetResource('DATA', 128); > RmveResource(dataHdl); > WriteResource(dataHdl); > DisposHandle(dataHdl); ^^^^^^^^^^^^^^^^^^^^^^ > > aHdl = NewHandle(DATA_SIZE); > /* At this point I init aHdl w/the data structure > * that I've maintained, in preparation for > * writing it back out. > */ > AddResource(aHdl, 'DATA', 128, ""); > WriteResource(aHdl); > DisposHandle(aHdl); ^^^^^^^^^^^^^^^^^^^ >} Never, ever call DisposHandle for a resource! Use ReleaseResource to de-allocate the memory that a resource is using. _______________________________________________________________________________ Dean Yu | E-mail: mystone@{sol,caen}.engin.umich.edu University of Michigan | Real-mail: Dean Yu Computer Aided Engineering Network | 909 Church St | Apt C ===================================| Ann Arbor, MI 48104 | Phone: Given on a need to know basis, and "I am the Merit Host. I speak for | only if you're going to offer me a the bitstream." (In other words, | job... these are my very own opinions; | my employer wants to have nothing |=========================================== to do with them, or me.) | This space available for rent -------------------------------------------------------------------------------