Looks OK on the surface. I would suggest you treat pointers that are NULL 
as not having memory allocated. So you must reset any pointer to NULL when 
it is freed throughout your code, and then in the final cleanup, only free 
non-NULL pointers. The global array initialise to zero is good.

You should #define g_source_array_count, and use this to allocate your 
global array. This way your loop will always work even if someone changes 
this value. Just don't try to set it to zero!

Not sure if you want to free memory in two arrays?

And you almost certainly don't want to allocate 100 characters on your stack.

(And use Char instead of char.)

Matt

At 11:13 PM 7/3/2002 +0000, you wrote:
>Hi group, got a question on global arrays that may be causing problems.
>
>  In globals.h, I have one defined as
>
>             extern char *sourcelist[100];
>
>In main.c, I declare it, and initialize it:
>
>            char *sourcelist[]  = {0};    // there's probably a better way
>
>
>I use this array of char pointers everywhere, in all of my c files, of
>course.
>
>
>  In mainforminit(), I fill it up with some srings,
>
>              sourcelist[index] = MemPtrNew(StrLen(theCat.catname) + 1);
>              StrCopy( sourcelist[index], theCat.catname );
>                                // ( theCat contains a structure just
>retrieved from
>                                // database, and catname is a string of
>course )
>
>
>And in other modules, I'll do things like erase one of the elements in the
>array with
>memptrfree, and then call MemPtrNew to reallocate and throw another string
>in there, etc...
>
>When my app is closed, I have a deAllocSource function that loops through
>the array of pointers, and calls
>MemPtrFree on every one, but I'm still intermittenly getting memory leakage
>errors when closing my app on the
>debug ROM. (using new POSE version, which tells me where I'm allocating the
>part of the memory that is leaking, but I still can't find the problem
>anyway)
>
>
>void deAllocSource()
>{
>     UInt16 index ;
>     UInt16 err;
>      char a[100];
>
>        for (index = 0; index < g_source_array_count; index++)
>     {
>
>       err = MemPtrFree(sourcel[index]);
>       ErrFatalDisplayIf(err, "problem with memptrfree'ing one of the sourcel
>array members");
>       err = MemPtrFree(sourcelist[index]);
>       ErrFatalDisplayIf(err, "problem with memptrfree'ing one of the source
>strings");
>
>
>     }
>       g_source_array_count = 0;
>       return;
>}
>
>
>Do you see a problem with using an array of pointers as a global variable
>like this?
>
>Thanks for reading this, would appreciate a hint if you have one
>Steve
>  p.s., using PALMOS 3.5 sdk with CW 6.0
>
>--
>For information on using the Palm Developer Forums, or to unsubscribe, 
>please see http://www.palmos.com/dev/support/forums/


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to