S
swarsa
Hi All,
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. Here is the problem I'm trying to solve:
I want to fill a list box with a list of Project Names from a database
(in Palm this is more similar to a file). The Palm OS function I must
call in order to get the Project Names to show up in the list box is
described below:
******************************************************************
LstSetListChoices Function
Purpose
Set the items of a list to the array of text string pointers passed to
this function. This functions erases the old list items.
Declared In
List.h
Prototype
void LstSetListChoices (
ListType *listP,
char **itemsText,
Int16 numItems
)
Parameters
- listP
Pointer to a list object.
- itemsText
Pointer to an array of text strings.
- numItems
Number of choices in the list.
Returns nothing.
******************************************************************
So, what I want to do is create the array of strings from the database.
Obviously, I don't know how many Project Names I will get from the
database or what size they'll be - so everything needs to be handled
dynamically.
So I define a global variable in the file like this:
static char **itemList;
I also have a structure defined that represents the record of a project
from the database as shown here:
typedef struct {
const char *projectName;
const char *projectDesc;
} ProjectDBRecord;
I also have a "packed" version of that structure defined that is how
the actual record of a project is stored in the database as shown here:
typedef struct {
char rec[1];
} PackedProjectDBRecord;
And I have a function that gets called when the project list needs to
be filled as shown here:
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);
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));
StrCopy(itemList[index + 1], rec.projectName);
MemPtrUnlock(project);
}
}
LstSetListChoices(lst, itemList, numRecs + 1);
LstDrawList(lst);
}
As you can see, the 'itemList' is initialized in this method.
When this function runs in POSE (Palm OS Emulator), it is telling me
that my program is writing to "a memory location (0x0003D062), which is
in the MemoryManager data structures.....Such an access usually means
that an application allocated a buffer (possibly with MemPtrNew) that
wasn't large enough for its purpose. When the application then tries to
write data to the buffer, it writes off the end of the buffer,
accessing the start of the buffer following it in memory." I see this
error displayed once for each of the following lines of code execute
when they execute:
itemList[0] = (char *) MemPtrNew(sizeof(defaultVal));
StrCopy(itemList[0], defaultVal);
Then finally the program dies with the following error message:
"During a regular checkup, Palm OS Emulator determined that the dynamic
heap chunk with the header address 0x0003D062 got corrupted. The size
of the chunk (%chunk_size) was larger than the currently accepted
maximum (%chunk_max)."
I'm sure I must be doing something obviously wrong here. Can anyone
give me a clue or point me in the right direction? If needed, I can
post the whole program. I just didn't want to "muddy up" the issue
with all the other event handing code. However, it might be helpful to
see the methods that pack and unpack a project, so I have posted them
here:
static void PackProject(ProjectDBRecord *project, MemHandle
projectDBHandle)
{
UInt16 length = 0;
char *s;
UInt16 offset = 0;
length = StrLen(project->projectName) +
StrLen(project->projectDesc) + 2;// 2 for string terminators
// resize the MemHandle and check for errors (0 means no error)
if(MemHandleResize(projectDBHandle, length) == 0)
{
// copy the fields
s = MemHandleLock(projectDBHandle);
DmWrite(s, offset, (char *) project->projectName,
StrLen(project->projectName) + 1);
offset += StrLen(project->projectName) + 1;
DmWrite(s, offset, (char *) project->projectDesc,
StrLen(project->projectDesc) + 1);
MemHandleUnlock(projectDBHandle);
}
}
static void UnpackProject(ProjectDBRecord *project, const
PackedProjectDBRecord *packedProject)
{
const char *s = packedProject->rec;
project->projectName = s;
s += StrLen(s) + 1;
project->projectDesc = s;
s += StrLen(s) + 1;
}
Thanks,
Steve Warsa
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. Here is the problem I'm trying to solve:
I want to fill a list box with a list of Project Names from a database
(in Palm this is more similar to a file). The Palm OS function I must
call in order to get the Project Names to show up in the list box is
described below:
******************************************************************
LstSetListChoices Function
Purpose
Set the items of a list to the array of text string pointers passed to
this function. This functions erases the old list items.
Declared In
List.h
Prototype
void LstSetListChoices (
ListType *listP,
char **itemsText,
Int16 numItems
)
Parameters
- listP
Pointer to a list object.
- itemsText
Pointer to an array of text strings.
- numItems
Number of choices in the list.
Returns nothing.
******************************************************************
So, what I want to do is create the array of strings from the database.
Obviously, I don't know how many Project Names I will get from the
database or what size they'll be - so everything needs to be handled
dynamically.
So I define a global variable in the file like this:
static char **itemList;
I also have a structure defined that represents the record of a project
from the database as shown here:
typedef struct {
const char *projectName;
const char *projectDesc;
} ProjectDBRecord;
I also have a "packed" version of that structure defined that is how
the actual record of a project is stored in the database as shown here:
typedef struct {
char rec[1];
} PackedProjectDBRecord;
And I have a function that gets called when the project list needs to
be filled as shown here:
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);
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));
StrCopy(itemList[index + 1], rec.projectName);
MemPtrUnlock(project);
}
}
LstSetListChoices(lst, itemList, numRecs + 1);
LstDrawList(lst);
}
As you can see, the 'itemList' is initialized in this method.
When this function runs in POSE (Palm OS Emulator), it is telling me
that my program is writing to "a memory location (0x0003D062), which is
in the MemoryManager data structures.....Such an access usually means
that an application allocated a buffer (possibly with MemPtrNew) that
wasn't large enough for its purpose. When the application then tries to
write data to the buffer, it writes off the end of the buffer,
accessing the start of the buffer following it in memory." I see this
error displayed once for each of the following lines of code execute
when they execute:
itemList[0] = (char *) MemPtrNew(sizeof(defaultVal));
StrCopy(itemList[0], defaultVal);
Then finally the program dies with the following error message:
"During a regular checkup, Palm OS Emulator determined that the dynamic
heap chunk with the header address 0x0003D062 got corrupted. The size
of the chunk (%chunk_size) was larger than the currently accepted
maximum (%chunk_max)."
I'm sure I must be doing something obviously wrong here. Can anyone
give me a clue or point me in the right direction? If needed, I can
post the whole program. I just didn't want to "muddy up" the issue
with all the other event handing code. However, it might be helpful to
see the methods that pack and unpack a project, so I have posted them
here:
static void PackProject(ProjectDBRecord *project, MemHandle
projectDBHandle)
{
UInt16 length = 0;
char *s;
UInt16 offset = 0;
length = StrLen(project->projectName) +
StrLen(project->projectDesc) + 2;// 2 for string terminators
// resize the MemHandle and check for errors (0 means no error)
if(MemHandleResize(projectDBHandle, length) == 0)
{
// copy the fields
s = MemHandleLock(projectDBHandle);
DmWrite(s, offset, (char *) project->projectName,
StrLen(project->projectName) + 1);
offset += StrLen(project->projectName) + 1;
DmWrite(s, offset, (char *) project->projectDesc,
StrLen(project->projectDesc) + 1);
MemHandleUnlock(projectDBHandle);
}
}
static void UnpackProject(ProjectDBRecord *project, const
PackedProjectDBRecord *packedProject)
{
const char *s = packedProject->rec;
project->projectName = s;
s += StrLen(s) + 1;
project->projectDesc = s;
s += StrLen(s) + 1;
}
Thanks,
Steve Warsa