[comp.sys.mac.programmer] Interesting problem with GetResource

dolf@fwi.uva.nl (Dolf Starreveld) (11/06/89)

I am currently working on a program that needs to copy a 'dctb' resource
from one file to another. Since the program must also copy other resources
I have written a general purpose "CopyResource" routine. The routine itself
was extensively tested and seemed to work, but .....
The copying of the 'dctb' resource was only recently added and things
started to go wrong. After lots of debugging I found out that after executing
the following statement:
	inHandle = GetResource('dctb', 10000);
"inHandle" contains a valid handle, but not a handle to a resource. I entered
TMON to find out more and:
1)	Just before executing the statement the 'dctb' resource is not
	loaded in the heap.
2)	Just after the statement, the 'dctb' resource is loaded, but
	immediately following it, the heap contains another relocatable
	block of exactly the same size of the resource (48 bytes).
3)	The GetResource call returns a handle to this last block instead
	of to the actually loaded resource.

Since my program continues to do a GetResInfo call on the handle returned,
I get into problems. The handle is not a handle to a resource and the
call sets ResError != 0. Furthermore, the &resID is set to -1 and the
&resType is set to four zero bytes. This is what finally causes another
program using the copied resource file to crash.

I moved the GetResource call to very early in my program (just after ToolBox
initialization), just to be sure the heap was not already corrupt. I also
tried using other values for the ID (100 instead of 10000). The net result
was the same in all cases: two blocks appear on the heap and the wrong
handle is returned to me.

Is this a bug in the system software? Am I doing something wrong?
Any suggestions?

BTW. For those who think it is important, I am using THINK C 4.0 on an
8Mb Mac IIcx, system 6.0.4b15.
Before you start screaming about using 6.0.4b15, you should know that
the problems were already there when I still used 6.0.3. I have not tested
with other versions.

--dolf

Dolf Starreveld  Phone: +31 20 592 5056/+31 20 592 5022, TELEX: 10262 HEF NL
EMAIL:           dolf@fwi.uva.nl (dolf%fwi.uva.nl@hp4nl.nluug.nl)
SNAIL:           Dept. of Math. and Computing Science, University of Amsterdam,
                 Kruislaan 409, NL-1098 SJ  Amsterdam, The Netherlands

han@apple.COM (Byron Han, Project Scapegoat) (11/07/89)

In article <232@fwi.uva.nl> dolf@fwi.uva.nl (Dolf Starreveld) writes:
> I have written a general purpose "CopyResource" routine. The routine 
itself
> was extensively tested and seemed to work, but .....
> The copying of the 'dctb' resource was only recently added and things
> started to go wrong. After lots of debugging I found out that after 
executing
> the following statement:
>         inHandle = GetResource('dctb', 10000);
> "inHandle" contains a valid handle, but not a handle to a resource. I 
entered
> TMON to find out more and:
> 1)      Just before executing the statement the 'dctb' resource is not
>         loaded in the heap.
> 2)      Just after the statement, the 'dctb' resource is loaded, but
>         immediately following it, the heap contains another relocatable
>         block of exactly the same size of the resource (48 bytes).
> 3)      The GetResource call returns a handle to this last block instead
>         of to the actually loaded resource.
>
> <deleted>
>
> Is this a bug in the system software? Am I doing something wrong?
> Any suggestions?

Well, this is kind of a strange quirk in the operating system.

Do this instead:
SetResLoad(FALSE);
hDCTB := GetResource('dctb', foobar);
SetResLoad(TRUE);
LoadResource(hDCTB);

This should work in place of a simple GetResource.  

+-------------------------------------------------------------------------+
| Disclaimer: Apple has no connection with my postings.                   |
+-------------------------------------------------------------------------+
Byron Han, CommToolbox Scapegoat    "DeAnza 3 - R.I.P. - 10/17/89 5:04PM"
Apple Computer, Inc.                
--------------------------------
20525 Mariani Ave, MS 69L         Internet: han@apple.COM
Cupertino, CA 95014               UUCP:{sun,voder,nsc,decwrl}!apple!han
--------------------------------  GENIE:BYRONHAN   CompuServe:72167,1664
ATTnet: 408-974-6450              Applelink:HAN1   HAN1@applelink.apple.COM
---------------------------------------------------------------------------

alan@Apple.COM (Alan Mimms) (11/07/89)

In article <232@fwi.uva.nl> dolf@fwi.uva.nl (Dolf Starreveld) writes:
>
>I am currently working on a program that needs to copy a 'dctb' resource
>from one file to another. Since the program must also copy other resources
>I have written a general purpose "CopyResource" routine. The routine itself
>was extensively tested and seemed to work, but .....
>The copying of the 'dctb' resource was only recently added and things
>started to go wrong. After lots of debugging I found out that after executing
>the following statement:
>	inHandle = GetResource('dctb', 10000);
>"inHandle" contains a valid handle, but not a handle to a resource. I entered
>TMON to find out more and:
>1)	Just before executing the statement the 'dctb' resource is not
>	loaded in the heap.
>2)	Just after the statement, the 'dctb' resource is loaded, but
>	immediately following it, the heap contains another relocatable
>	block of exactly the same size of the resource (48 bytes).
>3)	The GetResource call returns a handle to this last block instead
>	of to the actually loaded resource.
>
>Since my program continues to do a GetResInfo call on the handle returned,
>I get into problems. The handle is not a handle to a resource and the
>call sets ResError != 0. Furthermore, the &resID is set to -1 and the
>&resType is set to four zero bytes. This is what finally causes another
>program using the copied resource file to crash.
>
>I moved the GetResource call to very early in my program (just after ToolBox
>initialization), just to be sure the heap was not already corrupt. I also
>tried using other values for the ID (100 instead of 10000). The net result
>was the same in all cases: two blocks appear on the heap and the wrong
>handle is returned to me.
>
>Is this a bug in the system software? Am I doing something wrong?
>Any suggestions?
>
>BTW. For those who think it is important, I am using THINK C 4.0 on an
>8Mb Mac IIcx, system 6.0.4b15.
>Before you start screaming about using 6.0.4b15, you should know that
>the problems were already there when I still used 6.0.3. I have not tested
>with other versions.
>
>--dolf
>
>Dolf Starreveld  Phone: +31 20 592 5056/+31 20 592 5022, TELEX: 10262 HEF NL
>EMAIL:           dolf@fwi.uva.nl (dolf%fwi.uva.nl@hp4nl.nluug.nl)
>SNAIL:           Dept. of Math. and Computing Science, University of Amsterdam,
>                 Kruislaan 409, NL-1098 SJ  Amsterdam, The Netherlands

SOME version of the system software introduced a "feature" in which
'dctb' and 'actb' resources are CLONED when they're retrieved by GetResource
and related calls.  A colleague of mine discovered this recently.  You
MAY be able to get around this with either Get1Resource calls or with
the sequence

	SetResLoad (false);
	GetResource (...);
	SetResLoad (true);
	LoadResource (...);

Anyone in System Software care to comment?

Also, please note that you really SHOULD go ahead an upgrade to 6.0.4
final since there were a few bugs fixed in the last few versions of
6.0.4 after b15 (I THINK).

Hope this helps.
-- 

Alan Mimms                                      My opinions are generally
Communications Product Development Group        pretty worthless, but
Apple Computer                                  they *are* my own...
"The company has new jobs and Jobs has a new company" -- Harry Anderson

dolf@fwi.uva.nl (Dolf Starreveld) (11/07/89)

alan@Apple.COM (Alan Mimms) writes:

[Lots deleted]

> Also, please note that you really SHOULD go ahead an upgrade to 6.0.4
> final since there were a few bugs fixed in the last few versions of
> 6.0.4 after b15 (I THINK).

Thanks, both to you and Byron, for the info about dctb cloning.
The reason I have not yet upgraded to 6.0.4. final is that Apple Holland
is refusing to give it to the Dutch developers. They insist that you
need this only if you have a Portable or a IIci.
Anyway, tomorrow I am visiting a guy at Apple Holland and will try again.

--dolf


Dolf Starreveld  Phone: +31 20 592 5056/+31 20 592 5022, TELEX: 10262 HEF NL
EMAIL:           dolf@fwi.uva.nl (dolf%fwi.uva.nl@hp4nl.nluug.nl)
SNAIL:           Dept. of Math. and Computing Science, University of Amsterdam,
                 Kruislaan 409, NL-1098 SJ  Amsterdam, The Netherlands

pepke@loligo (Eric Pepke) (11/07/89)

In article <5058@internal.Apple.COM> han@apple.COM (Byron Han, Project Scapegoat) writes:
>
>Well, this is kind of a strange quirk in the operating system.
>
>Do this instead:
>SetResLoad(FALSE);
>hDCTB := GetResource('dctb', foobar);
>SetResLoad(TRUE);
>LoadResource(hDCTB);

Please give a complete definition of the quirk, including what types of 
resources it affects under what conditions.  I trust that I will not have
to change all my code that gets all kinds of resources to do this charade;
however, nor do I want to have timebombs ticking in my code.

Eric Pepke                                     INTERNET: pepke@gw.scri.fsu.edu
Supercomputer Computations Research Institute  MFENET:   pepke@fsu
Florida State University                       SPAN:     scri::pepke
Tallahassee, FL 32306-4052                     BITNET:   pepke@fsu

Disclaimer: My employers seldom even LISTEN to my opinions.
Meta-disclaimer: Any society that needs disclaimers has too many lawyers.

keith@Apple.COM (Keith Rollin) (11/15/89)

In article <338@vsserv.scri.fsu.edu> pepke@loligo.UUCP (Eric Pepke) writes:
>In article <5058@internal.Apple.COM> han@apple.COM (Byron Han, Project Scapegoat) writes:
>>
>>Well, this is kind of a strange quirk in the operating system.
>>
>>Do this instead:
>>SetResLoad(FALSE);
>>hDCTB := GetResource('dctb', foobar);
>>SetResLoad(TRUE);
>>LoadResource(hDCTB);
>
>Please give a complete definition of the quirk, including what types of 
>resources it affects under what conditions.  I trust that I will not have
>to change all my code that gets all kinds of resources to do this charade;
>however, nor do I want to have timebombs ticking in my code.

I haven't seen anyone respond to this yet, so I'll give what little information
I have. I hadn't responded earlier, as I don't know the full details of the
problem, and hate to speak knowledgably on things I don't know about.

I think that this whole thing was an attempt to fix a bug in the dialog 
manager. It was leaving 'dctb' resources in memory after the dialog was
disposed of. Apparently, the fix involved a patch to GetResource that would
detach the 'dctb'. This is what leads us to the current problem.

Needless to say, we in DTS were upset about this when we found out. There were
many developers that were messed up by this patch. I think that some guys here
are working on another fix that should fix the fix.

-- 
------------------------------------------------------------------------------
Keith Rollin  ---  Apple Computer, Inc.  ---  Developer Technical Support
INTERNET: keith@apple.com
    UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith
"Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions