[comp.sys.mac.hypercard] Scrambled Memory

Sharkey@ccu.umanitoba.ca (08/15/90)

Hi there :),

I've got an XFCN question, I wish to pass a larger string,
test(string)
from HyperCard to an XFCN, I am using Think C 4.02, my problem
is that, the XFCN will work the first time without problem, but  when
it is called the second time, Hypercard gives me an out of memory error
message or just crashes, I'm not out of memory, 5 megs on a IIcx, but
I believe I am seriously wierding out the stack.  Any ideas or
suggestions?



#include <MacTypes.h>
#include <ScriptMgr.h>
#include <IntlPkg.h>
#include <SetUpA4.h>
#include <string.h>
#include <HyperXCmd.h>

#define  NIL 0L
/* Hypercard parameter block:

  typedef struct XCmdBlock {
      short   paramCount;
      Handle  params[16];
      Handle  returnValue;
      Boolean     passFlag;

      void        (*entryPoint)();
      short       request;
      short       result;
      long        inArgs[8];
      long        outArgs[4];
   } XCmdBlock, *XCmdBlockPtr;
*/

pascal void main(paramPtr)
   XCmdBlockPtr    paramPtr;
{
   register   char   *s;             /* string ptr */
   Handle            srcH, dstH;     /* temp storage for Transliterate */

   /*** CODE STARTS HERE ***/

   RememberA0();                    /* A0 points to this procedure */
   SetUpA4();                       /* point A4 at this proc for literals */

  /*** Check parameters ***/

        if (paramPtr->paramCount != 1) {
                strcpy(*(paramPtr->returnValue), "argument error");
                return;
        }

        /*** initialize & set up return value ***/

        paramPtr->returnValue = NewHandle(6L);  /* set up return value */
        HLock(paramPtr->returnValue);
        HLock(paramPtr->params[0]);         /* lock param for de-reference */
        s = *(paramPtr->params[0]);         /* point to the string passed in */

        /*** Set up temp memory for Test ***/

        srcH = NewHandle(2L);
        dstH = NewHandle(1L);
        if (srcH == NIL || dstH == NIL) {
                strcpy(*(paramPtr->returnValue), "can't allocate tmp memory");
                if (srcH != NIL)
                        DisposHandle(srcH);
                if (dstH != NIL)
                        DisposHandle(dstH);
                return;
        }

        /*** MAIN LOOP ***/


                strcpy(*(paramPtr->returnValue), s);

        /*** Clean up ***/

        HUnlock(paramPtr->params[0]);
        HUnlock(paramPtr->returnValue);

        /* don't dispose of hItl here -- Intl Pkg owns it */

        DisposHandle(srcH);
        DisposHandle(dstH);
        RestoreA4();       /* point A4 back to where it was */

}

#include "XCmdGlue.inc.c"