[Gambas-user] How to use the WITH / END WITH safe

Benoit Minisini gambas at ...1...
Thu Apr 8 23:20:35 CEST 2004


On Thursday 08 April 2004 14:07, ron wrote:
> May be a silly question
>
> Example :
> WITH hButton
>   .Text = Cstr("Cancel" & 1/0 )
> END WITH
>
> is equivalent to
> hButton.Text = Cstr( "Cancel" & 1/0 )
>
> It is the bug VB has in my point of view.
> The WITH creates a reference to the hButton and allocate memory for it
> The END WITH stops the dot reference (.Text) and removes the reference
> and free the memory used for it.
>
> In the case of VB a error leaves the procedure and did
> not free the memory. Here for the example forced by divide by zero
>
> In VB you should use a construction like next to trap this miss behavior.
>
> ON Error Goto WithAbort
> WITH hButton
>   .Text = Cstr("Cancel" & 1/0 )
>
> WithAbort::
> ' free memory with next END
> END WITH
>
> I like to use the WITH a lot for example to set up controls in code.
> WITH Columnview1
> 	.color
> 	.columns
> 	.Height
> 	. the other position and dimensions
> END WITH
>  It is now easy to change the object name instead  without the WITH
> or even use the same code for a stack of ColumnViews
> Second pro is you do not need to type the name over and over.
>
> My question is: does gambas free used WITH references on exit of the
> procedure or must I use the same trick as VB needs?
>
> Ron
>

Don't worry Ron, Gambas is not VB :-) Everything is freed correctly.
If something is not freed correctly, then you will have a message in the 
console at program exit that tells you:

	WARNING: X allocation(s) non freed.

Then that means there is a memory leak somewhere. This is a bug, and you can 
call me :-)

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list