• 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: Newbie question: error in creating a NSData object using handle (Resource Management)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Ken Thomases <email@hidden>
  • Date: Thu, 26 Jun 2008 16:11:28 -0500

On Jun 25, 2008, at 9:49 PM, Tran Kim Bach wrote:

On Wed, Jun 25, 2008 at 3:36 PM, Ken Thomases <email@hidden> wrote


Also, in the pseudo-code you provide, the NSData objects will accumulate in
the autorelease pool until some point after your "for" loop. You can try
using an autorelease pool inside the loop so that the NSData objects are
released after each iteration. You may just be exhausting memory. For the
case where you're not using NSData, the memory exhaustion might not happen
since you're not storing the data twice in memory (once in the handle, once
in the NSData), but would if there were twice as many resources.

Thanks Ken. I'm confusing that as the memory management rules say, I myself
did not take the ownership of the NSData objects. So, I'm not responsible
for relinquishing ownership of them?

It is correct that you are not responsible for sending -release to the NSData object. However, you do need to understand the nature of autoreleased objects, the lifetime of autorelease pools, the fact that autoreleased objects accumulate in autorelease pools if you don't give the pools the opportunity to drain, and how to address that if it becomes a problem. The overview of the NSAutoreleasePool class reference says this:


If your application creates a lot of temporary autoreleased objects within the event loop, it may be beneficial to create local autorelease pools to help to minimize the peak memory footprint. You create an NSAutoreleasePool object with the usual alloc and init messages and dispose of it with release or drain (to understand the difference, see “Garbage Collection”). You should always drain an autorelease pool in the same context (invocation of a method or function, or body of a loop) that it was created. See Autorelease Pools <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concepts/AutoreleasePools.html > for more details and several code samples using autorelease pools.





I hope you can show me the way to get out of this problem. I'm totally new
to Programming in Mac OS.


       OSStatus error = noErr;

unsigned short pmResFile = 0;

NSString *resFile;

NSURL *url;

FSRef fsRef;

resFile = [textField stringValue]; // get resfile from a textField (for
example)


url = [NSURL fileURLWithPath:resFile];


CFURLGetFSRef( (CFURLRef)url, &fsRef);

(void)FSOpenResourceFile( &fsRef, 0, NULL, (SInt8)fsRdPerm, &pmResFile);

short saveRes = CurResFile();

if (pmResFile > 0)

{

int i = 0;

int count = 0;

ResType resType;

               NSString *typestr = [comboBox stringValue];

memcpy((char *)&resType, (char *)[typestr cStringUsingEncoding:
NSUTF8StringEncoding], 4);

The above is only correct for big-endian systems (PPC). Also, if the string is shorter than 3 characters, you'll access past then end of the character array.




UseResFile(pmResFile);

count = Count1Resources( resType );

for (i = 1; i <= count; i++)

{

long sizeLong;

short resIDShort;

NSString *name;

NSNumber *resID;

ResType resType1;

Str255 nameStr;

NSString *type;

Handle dataHandle;

NSData *data;

dataHandle = Get1IndResource( resType, i);

error = ResError();

if(error!=noErr)

{

NSLog(@"Reading resource error");

UseResFile(saveRes);

return;

}

GetResInfo(dataHandle, &resIDShort, & resType1, nameStr);

sizeLong = GetResourceSizeOnDisk(dataHandle);

You've been told to use GetHandleSize instead.



HLockHi( dataHandle );

name = [NSString stringWithCString:&nameStr[1] length:nameStr[0] ];

type = [NSString stringWithCString:(char *) & resType1 length:4];

Same endianness issue, in reverse.



data = [NSData dataWithBytes:*dataHandle length:sizeLong ];

       resID = [NSNumber numberWithShort:resIDShort];

if((type2 =='PREC')&&([resID intValue]== 302))

{

   struct PGControlRes pgControlRes;

memcpy(&pgControlRes,[data bytes], [data length]);

Have you tested that the resource size is the same as the structure size?




// more codes

}

HUnlock(dataHandle);

ReleaseResource(dataHandle);

}

UseResFile(saveRes);

}

CloseResFile(pmResFile);


Are you still getting an error?  If so, what error and where?

-Ken

_______________________________________________

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


  • Follow-Ups:
    • Re: Newbie question: error in creating a NSData object using handle (Resource Management)
      • From: "Tran Kim Bach" <email@hidden>
References: 
 >Newbie question: error in creating a NSData object using handle (Resource Management) (From: "Tran Kim Bach" <email@hidden>)
 >Re: Newbie question: error in creating a NSData object using handle (Resource Management) (From: Ken Thomases <email@hidden>)
 >Re: Newbie question: error in creating a NSData object using handle (Resource Management) (From: "Tran Kim Bach" <email@hidden>)
 >Re: Newbie question: error in creating a NSData object using handle (Resource Management) (From: Kai <email@hidden>)
 >Re: Newbie question: error in creating a NSData object using handle (Resource Management) (From: "Tran Kim Bach" <email@hidden>)

  • Prev by Date: NSTextFieldCell subclass template image inversion
  • Next by Date: Re: Good mouse tracking design for dynamic view?
  • Previous by thread: Re: Newbie question: error in creating a NSData object using handle (Resource Management)
  • Next by thread: Re: Newbie question: error in creating a NSData object using handle (Resource Management)
  • Index(es):
    • Date
    • Thread