On Thu, 17 Nov 2005, Del Ventruella wrote:
> I am attempting to sort records in a database based upon the magnitude of a
> number ("DegreeMatch" in structure "TransStruct"). I developed the following
> sort routine because the QuickSort option wasn't working consistently (even
> with a sort function).
>
> When I try to sort I tend to run into Null pointers if I don't check to
> confirm that handle's exist with the sort routine. If I do check for
> handles, and look at the contents of the database in a list box, the entries
> for which I search have no names associated in the list box, but there are
> spaces for them. I can click on the blank name spaces and get the records
> with names. My sort routine follows.
>
> I'd certainly appreciate any help getting this sort to work.
>
> Int16 SortThoseRecord(DmOpenRef dbase)
> {
> Int16 N= DmNumRecords(dbase);
> int minIndex, i, j,test;
> Err err;
> MemHandle x;
> MemHandle y;
> TransStruct* rec1 = (TransStruct *) MemPtrNew(sizeof(TransStruct));
> TransStruct* rec2 = (TransStruct *) MemPtrNew(sizeof(TransStruct));
> TransStruct* rec3 = (TransStruct *) MemPtrNew(sizeof(TransStruct));
> TransStruct* rec4 = (TransStruct *) MemPtrNew(sizeof(TransStruct));
Why are you doing the above 4 memory allocations? Your code doesn't appear
to use rec4 at all, and rec1 and rec2 are replaced by the MemHandleLock()
calls below.
> if (N >= 2)
> {
> for (i = 0; i < N-1; i++)
> {
> minIndex = i;
>
> x = DmGetRecord(gTransformer, i);
> if (x)
> {
> rec1 = (TransStructType *) MemHandleLock(x);
> MemHandleUnlock(x);
Why are you locking MemHandle x then immediately unlocking it? This will
mean that, after the MemHandleUnlock() call, rec1 won't be guaranteed to
point to anything, and referring to it will likely cause a crash.
> }
> // Find the index of the minimum element
> for (j = i; j < N-1; j++)
> {
>
> y = DmGetRecord(gTransformer, j);
> if (y)
> {
> rec2 = (TransStructType *) MemHandleLock(y);
> MemHandleUnlock(y);
See above comments about locking a MemHandle and immediately unlocking it.
> }
>
> if (rec2->DegreeMatch < rec1->DegreeMatch)
This statement is likely to crash, as both pointers are pointing to memory
that may or may not contain data of the type you expect it to. This will
probably crash, or cause unexpected non-crash behavior if you're lucky.
> {
> minIndex = j;
> }
> }
>
> // Swap if i-th element not already smallest
> if (minIndex > i)
> {
>
> x = DmGetRecord(gTransformer, i);
> if (x)
> {
> rec1 = (TransStructType *) MemHandleLock(x);
>
> *rec3 = *rec1;
> err = DmWrite(rec1, 0, rec2, sizeof(rec2));
Again, this is writing whatever rec2 is pointing to, which could be
anything, into rec1, which is a record in your database. sizeof(rec2)
probably isn't what you want to be doing here either.
> MemHandleUnlock(x);
> }
> y = DmGetRecord(gTransformer, minIndex);
> if (y)
> {
> rec2 = (TransStructType *) MemHandleLock(y);
>
> err = DmWrite(rec2, 0, rec3, sizeof(rec3));
> MemHandleUnlock(y);
> }
>
>
>
>
>
> }
> }
>
> }
>
> }
>
> --
> For information on using the PalmSource Developer Forums, or to unsubscribe,
> please see http://www.palmos.com/dev/support/forums/
>
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/