Peter,

That's a classic memory allocation/pointer question.

The memory is allocated by XtMalloc(). The result of the XtMalloc()
call is a pointer. You can keep as many copies of a pointer as you
like, this doesn't change the allocated memory.

The local variable (copy in the example below) is a temporary
storage; the widget field (mw->matrix.cells) is a more permanent
storage.

The allocated memory is freed in xbaeFreeCells(). To prevent
memory leaks from happening, xbaeFreeCells must be called prior
to widget destruction (before mw->matrix.cells looses its
permanent nature); or before overwriting mw->matrix.cells.

Hope this helps,

        Danny

> "Yang, Peter" wrote:
> 
> Would you please straight me out on following  question?  I was
> challenged by my friends here. And I can not provide a good answer to
> them. Thanks a bunch.
> 
> IN the code "Create.c >> xbaeCopyCells(XbaeMatrixWidget mw)", it was
> noticed that the  two dimension arrary "copy" is allocated , then
> assign the address to other variable. But the "copy" is not freed in
> this code.
> 
> Is it freed at somewhere else ??
> 
> See sample code below.
> 
> xbaeCopyCells(XbaeMatrixWidget mw)
> {
>     String **copy = NULL;
>     int i, j;
>     Boolean empty_row;
> 
>     xbaeObjectLock((Widget)mw);
> 
>     if (mw->matrix.rows && mw->matrix.columns)
>     {
>   /*
>    * Malloc an array of row pointers
>    */
>   copy = (String **) XtMalloc(mw->matrix.rows * sizeof(String *));
> <<<<<??? Allocate memory ????
> 
>     ......
>     ......< skip other code >
>     ......
> 
>  mw->matrix.cells = copy;                            <<<<<<<  assign
> memory location to someone else ????
> 
> 
> But who free it ?
> 
>     xbaeObjectUnlock((Widget)mw);
> }
_______________________________________________
Lesstif mailing list
[EMAIL PROTECTED]
https://terror.hungry.com/mailman/listinfo/lesstif

Reply via email to