RE: Newbie question: error in creating a NSData object using handle(Resource Management)
RE: Newbie question: error in creating a NSData object using handle(Resource Management)
- Subject: RE: Newbie question: error in creating a NSData object using handle(Resource Management)
- From: "Gary L. Wade" <email@hidden>
- Date: Wed, 25 Jun 2008 15:13:20 -0400
1. Have you set the resource file you're iterating over as the current resource file (UseResFile)? Make sure you save off the current resource file (CurResFile) before you do that so you can reset it once you close it. The Resource Manager is not good at keeping track of state in the way you may assume.
2. If you are iterating over the current resource file, use the "1"-named APIs: Count1Resources, Get1IndResource, etc. The non-"1"-named APIs, will fall back through other open resource files if a referenced resource in the current resource file is not present, and if you've got a Carbon-based runtime going, you may have thousands of resources available to you that aren't in the particular file you're looking at.
3. Use GetHandleSize to get the size of a Handle; GetResourceSizeOnDisk is important if you want the compressed size of a resource, which is probably not relevant anymore but was very important in System 7 days when files were too big to fit on disks, so Apple made it possible to compress resources to eek out some extra space, so a Handle, when brought into memory, could take up more space than it did on disk.
4. If your Handle has a NULL master pointer (NULL == *dataHandle), make sure you've called either SetResLoad (true) before getting the resource or calling LoadResource on the Handle you just got. In the old days of very limited memory resources, purging a resource handle was a great way to free up memory; after all, it could always be reloaded when needed. I'm not sure about the behavior of the Resource Manager under Cocoa, so the SetResLoad value may be false, thereby not pulling in all of the resource's data.
5. If all you're doing is copying bytes from a Handle into a structure of the same or smaller size than the Handle, the use of an NSData object is unnecessary and, at most, cause more memory to be used and time to be wasted; just use the master pointer and Handle length in your memcpy call. Of course, verify that your Handle is the same or smaller in size than your structure.
>Hi folks,I'm a newbie to Cocoa.
>Recently, I'm working on a project relating to Resource Management.
>In my project, there's a part that I'm reading through the resources in a
>resource file.
>I'm using:
>int count = CountResources( typeName );
>to get all resource that has the type "typeName", then loop through this
>resource list to take resource data out.
>
>for (n = 1; n <= count; n++)
>{
> Handle dataHandle = Get1IndResource( type1, n);
> ....
> NSData *data = [NSData dataWithBytes: *dataHandle length:
>GetResourceSizeOnDisk(dataHandle)]; // I GOT AN ERROR HERE
> //using data
> struct A_STRUCT aStruct;
>
> memcpy(& aStruct,[data bytes], [data length]);
>}
>After several times looping through the list, I got an error in the line
>above.
>But if I use data directly, like the following code, there is no error
>occurred.
> memcpy(&pgControlRes,*dataHandle, GetResourceSizeOnDisk(dataHandle));
>Any suggestions for my problem.
>I highly appreciate all your helps.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden