swarsa@msn.com wrote:[color=blue]
> I realize this is not a Palm OS development forum, however, even though
> my question is about a Palm C program I'm writing, I believe the topics
> are relevant here. This is because I believe the problem centers
> around my handling of strings, arrays, pointers and dynamic memory
> allocation.[/color]
There is at least one likely problem - but if this is really a problem
I can't tell because all the functions you use are non-standard functions,
so all people here can do is making guesses what they do and what kind of
arguments they expect, going by what some standard functions do that look
similar. But to get a reasonable answer you have to ask in a group that
deals with the specifics of the Palm OS - it's really off-topic here.
[color=blue]
> static void FillProjectList(ListType *lst)
> {
> UInt16 index;
> MemHandle h=0;
> UInt16 numRecs = DmNumRecordsInCategory(db, dmAllCategories);
> char defaultVal[] = "New...";
> itemList = (char **) MemPtrNew((sizeof(char *) * numRecs) + 1);
> itemList[0] = (char *) MemPtrNew(sizeof(defaultVal));
> StrCopy(itemList[0], defaultVal);[/color]
[color=blue]
> for(index = 0; index < numRecs; index++)
> {
> // get the handle to the record and set busy bit
> h = DmQueryRecord(db, index);
> if (h) { // could fail due to insufficient memory!
> PackedProjectDBRecord *project = MemHandleLock(h);
> ProjectDBRecord rec;
> UnpackProject(&rec, project);
> printf("Here is the project name: %s", rec.projectName);
> itemList[index + 1] = (char *)
> MemPtrNew(StrLen(rec.projectName));[/color]
If this MemPtrNew function works similar to malloc() and StrLen() is
like strlen() then you need to allocate one more char - strings in C
have a '\0' character at the end and that's not counted by strlen()
(please note that this is different from sizeof() when applied to a
string literal, as you did above with the string "New...").
[color=blue]
> StrCopy(itemList[index + 1], rec.projectName);[/color]
If it is the case that MemPtrNew() behaves like malloc(), StrLen()
like strlen() and StrCpy() like strcpy() then it would explain why you
get problems with memory corruption since you are always writing one
char past the end of the memory you allocated. Another thing that could
go wrong under these conditions is that MemPtrNew() returns a value that
tells you that you run out of memory, you must check for the possibility.
On the other hand, all this is pure guesswork and it would be much better
to ask in a group concerned with the idiosyncracies of Palm OS.
Regards, Jens
--
\ Jens Thoms Toerring ___
Jens.Toerring@physik.fu-berlin.de
\__________________________
http://www.toerring.de