mcdonald@fornax.UUCP (Ken Mcdonald) (03/13/90)
I'm writing some code which makes use of various resources (at them moment, mainly ICONs). I want it do dispose of whatever icons it loads, during its shutdown phase--but not if these icons are already loaded in, because then there is presumable some other code that is also using the icons. The way I came up with goes something like this: 1. Check to see of the needed icon is in memory. 2. If it is, make a copy of it (I assume there is some way to do this), so that the code that brought the icon in can safely dispose of its copy when necessary, and my code can safely dispose of its copy when necessary. 3. Otherwise, do a GetResource() on the icon, then a detach resource (so that code that doesn't do checks like this won't end up using my copy of the icon), then whatever else is necessary to get rid of the resource entry GetResource() created. 4. Finally, after my code terminates, call DisposHandle() to get rid of my icon. The only problem is that I can't find out how to perform step 1--that is, how to check to see if the resource manager has already loaded a particular resource. Seems silly, and no doubt I'm missing something obvious, but I'd appreciate it if someone could shed some light on the matter. Thanks, Ken McDonald mcdonald@cs.sfu.ca PS. Sorry for the typos--I don't really know how to use this editor, so its hard for me to do corrections.
brecher@well.sf.ca.us (Steve Brecher) (03/15/90)
In article <427@fornax.UUCP>, mcdonald@fornax.UUCP (Ken Mcdonald) writes: > I'm writing some code which makes use of various resources (at them > moment, mainly ICONs). I want it do dispose of whatever icons it > loads, during its shutdown phase--but not if these icons are already > loaded in, because then there is presumable some other code that is > also using the icons. > > The way I came up with goes something like this: > > 1. Check to see of the needed icon is in memory. > 2. If it is, make a copy of it (I assume there is > some way to do this), so that the code that > brought the icon in can safely dispose of its > copy when necessary, and my code can safely dispose > of its copy when necessary. > 3. Otherwise, do a GetResource() on the icon, then a > detach resource (so that code that doesn't do checks > like this won't end up using my copy of the icon), then > whatever else is necessary to get rid of the resource > entry GetResource() created. > 4. Finally, after my code terminates, call DisposHandle() > to get rid of my icon. > > The only problem is that I can't find out how to perform step 1--that is, > how to check to see if the resource manager has already loaded a particular > resource. Seems silly, and no doubt I'm missing something obvious... You're not missing anything. There is no supported way to do this. The only way to do it is to find the resource's entry in the resource map (without using the Resource Manager) and see if the handle field in the entry is nil. This subjects you to future compatibility problems should the format of resource maps change. The next best thing is something like this: SetResLoad(false); h := GetResource('ICON', theICONid); iconWasLoaded := (h^ <> nil); However, even if the resource data is not actually currently in RAM other software may have obtained the resource handle and hence you can not safely detach the resource. I presume you are talking about system resources to which these considerations apply. Depending on the context in which your software is running (you didn't mention whether it's an application, trap patch, etc.) and whether you know what file contains the resource, you may be able to get your own private copy of the resource map -- see Macintosh Technical Note #185. -- brecher@well.sf.ca.us (Steve Brecher)