[EMAIL PROTECTED] wrote:

> I realize this is a really novice question, so please forgive me (but that
> means it should be easy to answer).

> When you allocate space for a CharPtr array, like so:
>
> CharPtr someText = new Char[20];
>
> And then you pass that pointer to a palm function, such as:
>
> CtlSetLabel(labelPointer, someText);
>
> What is the proper way to avoid a memory leak?  Is the memory that was
> allocated freed automatically when the form is unloaded, or do I have to
> explicitly destroy it myself, as in:
>
> delete [] someText;

You have responsibility for deallocation and thus must do the above.

> If I try to destroy it myself immediately after I pass it to the function, I
> get any number of errors along the lines of:

That's 'cause the control is now using the pointer i.e. it didn't make a copy of your 
string into a private buffer.

> "The application has just read directly from an unallocated chunk of
> memory."
>
> It also occurs to me that I could place the code to free the memory in the
> form's unloading event, but I don't really think that's the right answer
> either.

Actually it is, so long as you allocated it when loading the form. Personally, I like 
using the ANSI auto_ptr class...

>  I have run into a number of areas where once I pass a pointer off
> to a system function, I can not safely free the memory, and I just want to
> make sure that something is handling it.

'hope this helps.

Kind regards,
Christopher
--
Christopher Hunt
Class Action Pty. Ltd.

Complete time zone management for the Palm(tm) connected organizer.
Check out http://www.classactionpl.com/


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

Reply via email to