"Jason Simpkins" <[EMAIL PROTECTED]> wrote:

> I am fighting weird freaky memory problems and it is driving me insane.
> Currently if I have large
> local char buffers in my functions, such as char buffer[1024], I am getting
> a "....tried to change the PC
> to address 0x3FE0098....address is niether in RAM or ROM....blah blah
> blah....".   If I declare the buffer as a global no more crashes....like the
> one above.  However, when reading the "companion" documention on the Palm
> web site, it advices, in one sentence to use globals, then four sentences
> later advises against it?!   WTFO?    What is the "offical" "smart" "best"
> way of declaring and using memory?  I mean, I'm trying to use standard K and
> R techniques here but just ain't working!

Use the heap. That way the only global variable(s) you need are the 
pointers to the memory you allocate when the app starts. Just remember 
to make your app exit gracefully if the allocate fails, and to have 
your app free the memory when it stops.

You can use the same technique for large amounts of variable data that 
is local to a form event handler. Here's a snippet from one of my apps:

static int MainHandleEvent(EventPtr event)
{
    static MemHandle    localdatah = 0;
    localdata_t        *localdata;

    switch (event->eType)
    {
        case frmOpenEvent:
            localdatah = MemHandleNew(sizeof(localdata_t));
            if (localdatah)
            {
                localdata = MemHandleLock(localdatah);
                // initialize elements of *localdata
                // maybe copy a record from a database into *localdata
                MemHandleUnlock(localdatah);
            }
            else
                // exit gracefully
            break;

        case ctlSelectEvent:
            localdata = MemHandleLock(localdatah);
            // use and/or modify *localdata
            MemHandleUnlock(localdatah);
            break;

        case frmCloseEvent:
            if (localdatah)
            {
                MemHandleFree(localdatah);
                localdatah = 0;
            }
            break;
}

--
Roger Chaplin
<[EMAIL PROTECTED]>

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palm.com/devzone/mailinglists.html

Reply via email to