druid wrote:
Run it twice in a row and it will crash
Does the record index change do to this routine
The rest of the DmWrite variables are global
and loaded from the read to the record


EditRecord()
 {
   UInt16 size = 0;
   UInt16 Offset = 0;
   Err err;
   Char *ptr;
err = Open();
  h = DmGetRecord(dbPtr, nRecordIndex);
  ptr = (Char *) MemHandleLock(h);
GetFieldData( levelFLD, level, 255 );
  GetFieldData( expiFLD, expFld, 255 );
  GetFieldData( repeatFLD, repet, 255 );
  GetFieldData( pointsFLD, spoints, 255 );
  GetFieldData( completeFLD, complete, 255 );
  GetFieldData(  mItemFLD, mItems, 255 );
   size = StrLen(name)+1+ StrLen(level)+1+ StrLen(expFld)+1+ StrLen(desc)+1+
   StrLen(mItems)+1+ StrLen(startz)+1+ StrLen(heritage)+1+ StrLen(citytask)+1+
   StrLen(sabatoge)+1+ StrLen(repet)+1+ StrLen(access)+1+ StrLen(freeport)+1+
   StrLen(qeynos)+1+ StrLen(complete)+1+ StrLen(comment)+1+ StrLen(spoints)+1+
   StrLen(npc)+1+ StrLen(step1)+1+ StrLen(step2)+1+ StrLen(step3)+1+ 
StrLen(step4)+1+
   StrLen(priorq)+1;

You need to put a DmResizeRecord() here.  You've just possibly changed
the size of the record and then you go writing everything into the
existing record without any check whether the record is big enough.

Actually, it's a bit more complicated than that, because when you do
DmResizeRecord(), it can change the handle, so you need to do use the
handle that DmResizeRecord() returns.

So basically, leave the DmGetRecord() where it is, then do

        h = DmResizeRecord (dbPtr, nRecordIndex, size);
        ptr = (Char *) MemHandleLock(h);

right after you compute the size, and remove the MemHandleLock()
call above.

By the way, it would be a lot cleaner and less error prone to make a
big list of string pointers and then use a loop.  If you use the same
list to compute the size and to write everything to the record, this
would ensure that a typo doesn't leave you with an incorrectly computed
size.  For example:

        Char *stringlist[] =
        {
            name, level, expFld, desc, mItems, startz, heritage, citytask,
            sabatoge, repet, access, freeport, qeynos, complete, comment,
            spoints, npc, step1, step2, step3, step4, priorq,
            NULL
        };
        Char **stringptr;

        size = 0;
        for (stringptr = stringlist; *stringptr != NULL; stringptr++)
        {
            size += 1 + StrLen (*stringptr);
        }

        /* do all your DmResizeRecord(), MemHandleLock() stuff here */

        offset = 0;
        for (stringptr = stringlist; *stringptr != NULL; stringptr++)
        {
            DmWrite (ptr, offset, *stringptr, 1 + StrLen (*stringptr));
            offset += 1 + StrLen (*stringptr);
        }

Also, the code is much shorter that way...

  - Logan

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

Reply via email to