"Stan Hunter" <[EMAIL PROTECTED]> wrote in message
news:52757@palm-dev-forum...
>
> I'm going nuts. I can't see what is wrong here, but when I call
MemMove from
> a subFunction, the stack disappears in the debugger and when control
returns
> to the calling function (saveWSTCell), MemMove calls say I have just
written
> to an unallocated chunk of memory. If I comment the MemMove out of the
> subFunction, the calling function is no longer messed up.

dummyString is stored on the stack, as is the pointer that is stored at
function entry that the debugger uses to find the caller function.

> If anyone can help me out, it'd save me pulling out a lot more hair.
> Thanks. -Stan.
>
> static Boolean saveWSTCell( VoidPtr table, Word row, Word column )
> {
> /* some irrelevant lines */
>  if ( field && FldDirty( field ) )
>  {
>   VoidHand h = gHandles[(set - 1)][record];
>   CharPtr  s;
>   int   i;
>   CharPtr  precord;
>   Int   offset;
>   Char  dummyString[ DB_SET_STRING_SIZE ];
>   Int   numSets;
>
>   // Store string in database
>   hrecord = DmGetRecord( reflexDB, record );
>   precord = MemHandleLock( hrecord );
>   offset = subFunction( column, record ); // offset gets correct value
(86)
>   DmWrite( precord, offset, s, StrLen( s ) + 1 );
>   MemHandleUnlock( hrecord );
>   DmReleaseRecord( reflexDB, record, true );
>
>   // Just to see what it put in the record
>   offset = subFunction( column, record );
>   hrecord = DmQueryRecord( reflexDB, record );
>   precord = MemHandleLock( hrecord );
>
>   /* FAILS here -- writing to unallocated memory!? */
>   MemMove( &dummyString, precord + offset, DB_SET_STRING_SIZE );
>   MemHandleUnlock( hrecord );

The MemMove would cause corruption if DB_SET_STRING_SIZE was longer than
the number of bytes allocated to dummyString.  However, since its
allocated with that length, this seems like it should be OK.

Here's a technique that can be used to track this down.  Before you
execute the MemMove, open a memory window on the dummyString variable
(you can do this by right clicking on the variable name and choosing
"Show Memory").  Then, step over the MemMove and see what happened in
the memory window.  It would probably help if you did a MemSet first to
clear out the buffer so you could easily tell what bytes got written.

>   TblMarkRowInvalid( table, row );
>   result = true;
>  }
>  return result;
> }
>
> // Takes a column and record and returns appropriate DB offset
> static Int subFunction( Word column, Int record )
> {
>  Int   numSets;
>  CharPtr  precord;
>  Char  dummyString[ DB_SET_STRING_SIZE ];
>
>  hrecord = DmQueryRecord( reflexDB, record );
>  precord = MemHandleLock( hrecord );
> /* If I comment out this next MemMove line, the stack does not
disappear and
> the callingFunction is not messed up.*/
>  MemMove( &numSets, precord + DB_NUM_SETS_START, DB_NUM_SETS_SIZE );
>  MemHandleUnlock( hrecord );
>
>  return 86;    // For debugging purposes, I hard-coded the correct
value to
> return.
> }




-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to