[comp.sys.mac.programmer] Problem with GetString? Or is it me?

summer@sarcasm.trl.oz.au (Mark Summerfield) (01/16/91)

In my latest project I am trying to do all the "right things", and
am using 'STR ' resources for absolutely all text. In particular,
my error alert code looks like this:

void ErrorAlert(alrtID, strID)
	short		alrtID, strID;
{
	StringHandle	theString;

	if ((theString = GetString(strID)) == nil) {
		/* Handle missing resource case... */
	}

	HLock(theString);
	ParamText(*theString, nil, nil, nil);
	(void) CentreStopAlert(alrtID, nil);
		/* A function of mine that behaves exactly like StopAlert()
		   except that at reads in the resource and centres the
		   dialog on the screen before displaying it... works fine */
	HUnlock(theString);	/* Probably unnecessary */
	DisposHandle(theString);
}

This works fine the first time I use it for any given strID, the problem
is that if it is called a second time (at any later stage) with a strID
that has been used before (ie if the same error occurs twice) then the
handle returned by GetString() points to an empty string! I've checked this
with SADE -- I'm not just guessing from the empty dialog box :-)

What am I missing here? Please e-mail, our feed is still very poor.

Thanks in advance,

Mark.
------------------------------------------------+-----------------------------+
Mark Summerfield, Telecom Research Laboratories |     Heisenberg may have     |
ACSnet[AARN/Internet]: m.summerfield@trl.oz[.au]|         been here!          |
Snail: PO Box 249, Clayton, Vic., 3168          +-----------------------------+

ech@cbnewsk.att.com (ned.horvath) (01/17/91)

From article <1991Jan16.020012.24103@trl.oz.au>, by summer@sarcasm.trl.oz.au (Mark Summerfield):
...
> 	if ((theString = GetString(strID)) == nil) {
> 		/* Handle missing resource case... */
> 	}

> 	HLock(theString);
> 	ParamText(*theString, nil, nil, nil);
...		[display alert]
> 	HUnlock(theString);	/* Probably unnecessary */
> 	DisposHandle(theString);
...
> This works fine the first time I use it for any given strID, the problem
> is that if it is called a second time (at any later stage) with a strID
> that has been used before (ie if the same error occurs twice) then the
> handle returned by GetString() points to an empty string! I've checked this
> with SADE -- I'm not just guessing from the empty dialog box :-)

DON'T use DisposHandle -- this is a resource, so use ReleaseResource to
indicate you're done with it.  Incidentally, ParamText makes a copy, so
you can HUnlock and ReleaseResource immediately after the ParamText call --
you don't have to keep it around for the Alert.

Also, you might want to call LoadResource right after GetString: it's
possible that the handle for the string is in memory, but the contents
have been purged.  LoadResource does nothing if the content is present,
and loads the content if it isn't, so the call is cheap insurance.

=Ned Horvath=
ehorvath@attmail.com

ech@cbnewsk.att.com (ned.horvath) (01/17/91)

> 	if ((theString = GetString(strID)) == nil) {

Correction: in my last posting, I suggested using 
	LoadResource (theString);
to ensure that the contents were in memory.  IM-1 claims that GetString
will do that for you, so the LoadResource is unnecessary.

There is a point to be made here, though: for resource types without
a specific "Get..." routine, it's best to follow a (successful) call
to GetResource with a call to LoadResource, in case the resource was 
previously loaded and purged.

=Ned Horvath=
ehorvath@attmail.com

maa@clinet.fi (Miika Asunta) (01/17/91)

ech@cbnewsk.att.com (ned.horvath) writes:

>> 	if ((theString = GetString(strID)) == nil) {


Hey hey hey....

Why to use 'STR ' resources? 'STR#' is more convenient, because you
don't have to use resources directly:

{
Str255 string;

GetResource(string,1000,1);
ParamText(stringi,0L,0L,0L);

}
-- 
*************************************************************************
* Miika Asunta		        *	Double Basist			*
* internet: maa@clinet.fi       *	     Macintosh Programmer	*
* UUCP: mcsun!fuug!clinet!maa   *					*

djvelleman@amherst.bitnet (01/18/91)

In article <1991Jan16.202350.4238@cbnewsk.att.com>, ech@cbnewsk.att.com (ned.horvath) writes:
>> 	if ((theString = GetString(strID)) == nil) {
> 
> Correction: in my last posting, I suggested using 
> 	LoadResource (theString);
> to ensure that the contents were in memory.  IM-1 claims that GetString
> will do that for you, so the LoadResource is unnecessary.
> 
> There is a point to be made here, though: for resource types without
> a specific "Get..." routine, it's best to follow a (successful) call
> to GetResource with a call to LoadResource, in case the resource was 
> previously loaded and purged.

  IM-1 also claims that GetResource will load the resource data into memory
if it's not in memory, so I don't see the need for LoadResource in this case
either (unless you've called SetResLoad(false)).  Or am I misinterpreting IM?

  Dan Velleman