Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-24 Thread Tran Kim Bach
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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-24 Thread Kai

Hi,

"I GOT AN ERROR HERE" may be a little too unspecific. Perhaps you can  
elaborate: crash, exception, nil return, Console entry, what else?


That said, you should use GetHandleSize (dataHandle) instead of  
GetResourceSizeOnDisk(dataHandle). GetResourceSizeOnDisk() can return  
values which are too large (see documentation).


Ah, and I just see that you combinded CountResources with  
Get1IndResource, that’s probably wrong. You should use CountResources  
with GetIndResource or Count1Resources with Get1IndResource.


Neither of this directly explains the difference between using NSData  
and the direct copy. But since you potentially access non existing  
bytes or data, that may be coincidence.


Best
Kai


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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/kai%40granus.net

This email sent to [EMAIL PROTECTED]


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-24 Thread Ken Thomases

On Jun 25, 2008, at 1:19 AM, Tran Kim Bach wrote:


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


What do you mean "GOT AN ERROR"?  What error?  How did it manifest?




  //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.


You don't show if you're:

1) Checking if dataHandle is NULL
2) Checking if *dataHandle is NULL
3) If (1) or (2), checking ResError()
4) Disposing of dataHandle with ReleaseResource()

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.


Cheers,
Ken
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-25 Thread Tran Kim Bach
Thanks Ken and Kai for your very very quick responsesThis is my first post
in the list, I'm sorry for not clarifying my problem.

Actually, the program stopped at the mentioned line.
In console, it said something like:

*objc[2144]: FREED(id): message release sent to freed object=0x17d1d0*

This GDB was configured as "i386-apple-darwin"
(Error from the debugger) /users/...path_to_my_exe_file: No such file or
directory.

But I checked the exe file and it's absolutely there(in the bundle).
I tried an autorelease pool inside the loop, disposing of datahandle.etc,
but still got the same problem.
Now, when I confirmed the *dataHandle, it returned NULL
but I don't know why?

--Bach

On Wed, Jun 25, 2008 at 3:36 PM, Ken Thomases <[EMAIL PROTECTED]> wrote:

> On Jun 25, 2008, at 1:19 AM, Tran Kim Bach wrote:
>
>  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
>>
>
> What do you mean "GOT AN ERROR"?  What error?  How did it manifest?
>
>
>
>>  //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.
>>
>
> You don't show if you're:
>
> 1) Checking if dataHandle is NULL
> 2) Checking if *dataHandle is NULL
> 3) If (1) or (2), checking ResError()
> 4) Disposing of dataHandle with ReleaseResource()
>
> 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.
>
> Cheers,
> Ken
>
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-25 Thread Kyle Sluder
On Wed, Jun 25, 2008 at 4:42 AM, Tran Kim Bach <[EMAIL PROTECTED]> wrote:
> Actually, the program stopped at the mentioned line.
> In console, it said something like:
>
> *objc[2144]: FREED(id): message release sent to freed object=0x17d1d0*
>
> This GDB was configured as "i386-apple-darwin"
> (Error from the debugger) /users/...path_to_my_exe_file: No such file or
> directory.
>
> But I checked the exe file and it's absolutely there(in the bundle).

What is this "exe file" nonsense?  I don't have any files with the
extension "exe" on my machine.  I'd wager you don't either.  Seems
like you're a Windows programmer.  In that case I heartily recommend
you read up on the differences between the way Windows executables and
UNIX binaries function -- sometimes subtle.  Then read up on .app
packages.

> I tried an autorelease pool inside the loop, disposing of datahandle.etc,
> but still got the same problem.

You're debugging by voodoo.

> Now, when I confirmed the *dataHandle, it returned NULL
> but I don't know why?

Post the code that supposedly assigns something to dataHandle, and we
might be able to help you.

--Kyle Sluder
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-25 Thread Hamish Allan
On Wed, Jun 25, 2008 at 2:08 PM, Kyle Sluder
<[EMAIL PROTECTED]> wrote:

> On Wed, Jun 25, 2008 at 4:42 AM, Tran Kim Bach <[EMAIL PROTECTED]> wrote:
>
>> But I checked the exe file and it's absolutely there(in the bundle).
>
> What is this "exe file" nonsense?  I don't have any files with the
> extension "exe" on my machine.  I'd wager you don't either.

To be fair, one might use the word "exe" as an abbreviation for "executable"!

>> Now, when I confirmed the *dataHandle, it returned NULL
>> but I don't know why?
>
> Post the code that supposedly assigns something to dataHandle, and we
> might be able to help you.

He did that in an earlier email:

Handle dataHandle = Get1IndResource( type1, n);

The documentation for Get1IndResource() has two notes of particular interest:

1) If it returns NIL [sic], the ResError() function can give you more info.
2) It's a legacy function and you should be using something else.

Someone who knows more about these things than I do may be able to
point you in the direction of a more modern way of accessing resources
:)

Hamish
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-25 Thread Andrew Farmer

On 25 Jun 08, at 06:43, Hamish Allan wrote:

Someone who knows more about these things than I do may be able to
point you in the direction of a more modern way of accessing  
resources :)


There is, in fact, no "more modern way" of accessing resources. The  
preferred alternative is to use individual files in the Resources  
folder of your application package. Whether this is appropriate or not  
in Tran's case depends on what his goal is, though...

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-25 Thread Kai


On 25.6.2008, at 10:42, Tran Kim Bach wrote:


Thanks Ken and Kai for your very very quick responses
This is my first post in the list, I'm sorry for not clarifying my  
problem.


Actually, the program stopped at the mentioned line.
In console, it said something like:
objc[2144]: FREED(id): message release sent to freed object=0x17d1d0


Ok, this looks like a standard overrelease problem. You posted too  
little code to include the problematic point.


Best to read up on Cocoas Memory Management Rules and make sure, that  
your Objective-C objects are not overreleased. Enabling Zombies can  
help if you can’t find it in source.


(Note: retain counts are the same concept as AddRef and Release in  
Microsofts COM, which you may know.)


Unless you have exceptionally many or large resources, I’d say you  
won’t need to worry about running out of memory in the loop on todays  
machines.


Best
Kai

PS: In case you have more questions about old-style resource access,  
you’d better take them to Apples Carbon list. People there are  
probably more understanding about the need to use time proven but no  
longer fancy APIs. ;-)



This GDB was configured as "i386-apple-darwin"
(Error from the debugger) /users/...path_to_my_exe_file: No such  
file or directory.


But I checked the exe file and it's absolutely there(in the bundle).
I tried an autorelease pool inside the loop, disposing of  
datahandle.etc, but still got the same problem.

Now, when I confirmed the *dataHandle, it returned NULL
but I don't know why?

--Bach

On Wed, Jun 25, 2008 at 3:36 PM, Ken Thomases <[EMAIL PROTECTED]>  
wrote:

On Jun 25, 2008, at 1:19 AM, Tran Kim Bach wrote:

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

What do you mean "GOT AN ERROR"?  What error?  How did it manifest?




 //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.

You don't show if you're:

1) Checking if dataHandle is NULL
2) Checking if *dataHandle is NULL
3) If (1) or (2), checking ResError()
4) Disposing of dataHandle with ReleaseResource()

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.


Cheers,
Ken



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-25 Thread I. Savant
> PS: In case you have more questions about old-style resource access,
> you'd better take them to Apples Carbon list. People there are
> probably more understanding about the need to use time proven but no
> longer fancy APIs. ;-)

  Not that you're bitter, though ... ;-)

--
I.S.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


RE: Newbie question: error in creating a NSData object using handle(Resource Management)

2008-06-25 Thread Gary L. Wade
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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle(Resource Management)

2008-06-25 Thread Jens Alfke




 Handle dataHandle = Get1IndResource( type1, n);
 


I hope you checked that dataHandle!=NULL and *dataHandle!=NULL.


 struct A_STRUCT aStruct;
 memcpy(& aStruct,[data bytes], [data length]);


This is dangerous —  if [data length] is larger than sizeof(aStruct),  
you've just clobbered your stack. If it's smaller, you've left part of  
aStruct uninitialized with garbage bytes in it. Either of those  
situations is likely to cause a crash, especially the former because  
it can very easily be abused by hackers to take control of your app.


After several times looping through the list, I got an error in the  
line

above.


What error, specifically? How are we supposed to tell you how to  
correct the error if you won't tell us what it is?!


—Jens___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-25 Thread Tran Kim Bach
Thanks everybody for your kind helps.
I'm so happy that all of you welcome me warmly here.
On Wed, Jun 25, 2008 at 3:36 PM, Ken Thomases <[EMAIL PROTECTED]> 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?
On Wed, Jun 25, 2008 at 10:08 PM, Kyle Sluder <
[EMAIL PROTECTED] <[EMAIL PROTECTED]>> wrote:
>
>
> What is this "exe file" nonsense?  I don't have any files with the
> extension "exe" on my machine.  I'd wager you don't either.  Seems
> like you're a Windows programmer.  In that case I heartily recommend
> you read up on the differences between the way Windows executables and
> UNIX binaries function -- sometimes subtle.  Then read up on .app
> packages.

I meant the executable file inside the app package(under contens/MacOS/) not
the file with the extension .exe.
But you're right. I has just started to use Mac OS for 3 months :). I will
try to read up your suggested information.
On Thu, Jun 26, 2008 at 12:58 AM, Kai <[EMAIL PROTECTED]> wrote:

>
> On 25.6.2008, at 10:42, Tran Kim Bach wrote:
>
>  Thanks Ken and Kai for your very very quick responses
>> This is my first post in the list, I'm sorry for not clarifying my
>> problem.
>>
>> Actually, the program stopped at the mentioned line.
>> In console, it said something like:
>> objc[2144]: FREED(id): message release sent to freed object=0x17d1d0
>>
>
> Ok, this looks like a standard overrelease problem. You posted too little
> code to include the problematic point.
>
> Best to read up on Cocoas Memory Management Rules and make sure, that your
> Objective-C objects are not overreleased.

I did read it but maybe I did not get it all. Below is my entire code with
some unrelated codes removed.
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);

 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);

HLockHi( dataHandle );

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

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

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]);

// more codes

}

HUnlock(dataHandle);

ReleaseResource(dataHandle);

}

UseResFile(saveRes);

}

CloseResFile(pmResFile);



Cheers,
Bachtk
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-26 Thread Tran Kim Bach
>
>
>  if((type2 =='PREC')&&([resID intValue]== 302))
>

There is a typo in the above code line:It should be:

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

Regards,
Bachtk
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-26 Thread Ken Thomases

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

On Wed, Jun 25, 2008 at 3:36 PM, Ken Thomases <[EMAIL PROTECTED]>  
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  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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-26 Thread Tran Kim Bach
Wow, thanks Ken a lot.About the endian issues, I have a compatible swap
function to convert data to Big-endian (and vice verse).
Then, I will correct big-endian issues.
I may know where the problem is.
When I read to your suggestion,

On Fri, Jun 27, 2008 at 6:11 AM, Ken Thomases <[EMAIL PROTECTED]> wrote:
>
>
>>
>> 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?


I found out something.
Actually, I'm just trying to make a prototype in one fixed size of the same
resource type.
I used to test on the same resource files with this fixed resource
structure.
but in reality, in my application, the resource size for this type(PREC) is
various for each resource file(.rsrc).
So, I think the struct caused my problem ( I never thought of it before).
By the way, I have a lot of structs of the same resource type(PREC for
example) written in C,
Is there any way to still take advantage of them in Objective-C?
(I mean if I can use these C structs in Objective-C?, don't have to rewrite
them using Objective-C language).
Thank you for your supports.

---(Bachtk
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-26 Thread Graham Cox

Just an aside that may or may not be relevant.

In the old days, often variable-length resources and other OS types  
would have a struct defined for them that used a fixed field size to  
represent some variable length field in the real resource, for example:


typedef struct
{
short count;
long  stuff[1]; // in reality  length
}
someResTypeStruct;

This is simply because you can't express variable length structs in C.  
So using sizeof(someResTypeStruct) == GetHandleSize(anActualResource)  
very often doesn't work, never has worked, and definitely shouldn't be  
relied upon. Same goes for allocating memory for such resources, which  
is why many common types had their own NewXXX function.


That said, you can use C structs in Objective-C normally, just be  
aware that they might only describe part of the real resource itself  
(the header, say).




hth,

Graham


On 27 Jun 2008, at 1:00 pm, Tran Kim Bach wrote:

but in reality, in my application, the resource size for this  
type(PREC) is

various for each resource file(.rsrc).
So, I think the struct caused my problem ( I never thought of it  
before).

By the way, I have a lot of structs of the same resource type(PREC for
example) written in C,
Is there any way to still take advantage of them in Objective-C?
(I mean if I can use these C structs in Objective-C?, don't have to  
rewrite

them using Objective-C language).


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: Newbie question: error in creating a NSData object using handle (Resource Management)

2008-06-27 Thread Kai


On 27.6.2008, at 05:00, Tran Kim Bach wrote:


Wow, thanks Ken a lot.
About the endian issues, I have a compatible swap function to  
convert data to Big-endian (and vice verse).

Then, I will correct big-endian issues.
I may know where the problem is.
When I read to your suggestion,

On Fri, Jun 27, 2008 at 6:11 AM, Ken Thomases <[EMAIL PROTECTED]>  
wrote:



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?


I found out something.
Actually, I'm just trying to make a prototype in one fixed size of  
the same resource type.
I used to test on the same resource files with this fixed resource  
structure.
but in reality, in my application, the resource size for this  
type(PREC) is various for each resource file(.rsrc).
So, I think the struct caused my problem ( I never thought of it  
before).
By the way, I have a lot of structs of the same resource type(PREC  
for example) written in C,

Is there any way to still take advantage of them in Objective-C?


Absolutely. Objective-C is a superset of C, so you can use all C you’d  
ever like to use.


Code of you like
NSNumber* resID =   [NSNumber numberWithShort:resIDShort];
and later
[resID intValue]
is pretty unnecessary - unless you are going to use resID with some  
Cocoa message which expects an NSNumber.


Another tip: Handle locking (HLockHi, HUnlock) is a no op under Mac OS  
X and can be skipped.


Best
Kai

(I mean if I can use these C structs in Objective-C?, don't have to  
rewrite them using Objective-C language).

Thank you for your supports.

---(Bachtk


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]