On Fri, 2003-08-01 at 22:59, R�gis Daniel de Oliveira wrote:
> Does anybody know which is the best way to fill a list with a lot of itens?
>
> First of all, i tried to use a pointer to store all my itens, like the code
> below
There's two approaches:
1) get the code you're writing to do what you want, and include error
checking. You're allocating way too much memory for what you need, and
I'm guessing that at some point the allocation is failing, but you're
assigning to it anyway because you're not checking for that failure, nor
do you have any way to inform the calling function that there was a
failure.
I really don't recommend taking this approach at all. The Palm devices
have a precious and small stack to allocate memory from. Once you've
over-allocated, that's it, it's gone and your program crashes.
Instead of filling the list (an array of strings, not the list widget)
at all, leave all the strings in the database, and go get them when you
need them one at a time, rather than trying to load them all up in one
function and returning the block.
You'll save a lot of memory and time, and your application will run
faster (depending on how often you access them).
As far as the function you've written, your code is not doing what I
think you want.
When you're allocating memory for the itemList, it should look something
like this:
[...]
// First, get a block of memory that can hold RecNum pointers.
// remember, the itemList is a pointer to a set of pointers
// You were allocating 240 times as much space as you needed for this!
itemList = (Char **)(MemPtrNew(RecNum*sizeof(Char *));
[...]
for (Cont =0; Cont < RecNum; Cont ++)
{
if ((h = DmQueryRecord(dbPtrF, Cont))) {
if ((CodProd = (Char *)MemHandleLock(h))) {
ForwardColumnsOnRecord(CodProd, &Descricao, 1);
MemHandleUnlock(h);
// Attempt the allocation of the string. Iff you get it, you
// can write into it.
// Note that for each string, you were allocating 4 times
// as much memory as you needed.
if ((itemList[Cont]=
(Char *)MemPtrNew(StrLen(Descricao)*sizeof(Char)+1))) {
StrCopy(itemList[Cont], Descricao);
} else {
FrmCustomAlert(alrtError,"can't allocate string memory"," "," ");
}
} else {
FrmCustomAlert(alrtError,"can't lock record."," "," ");
}
} else {
FrmCustomAlert(alrtError,"can't get record."," "," ");
}
}
Note that everything is in nested if statements? That's *necessary* so
that when something fails you can be told why.
I'd bet this function will 'work' (though I typed it by hand and didn't
compile it), but read my comments above about why you shouldn't use this
approach.
-Ken
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/