[Gambas-devel] Error Handling and Type Testing

Benoît Minisini gambas at ...1...
Wed Feb 8 08:21:55 CET 2012


Le 06/02/2012 13:05, Randall Morgan a écrit :
> Hi Benoit,
>
> I'd like to ask you about how best to handle errors.

You must use the GB.Error() function that takes the error string as 
argument, or one of the GB_ERR_* predefined error constants (look in 
'gambas.h').

GB.Error() can takes up to four optional substitution argument, like the 
Subst$() function.

> How to report them
> to back to Gambas,

Once you have call GB.Error(), you must return from the implementation 
function explicitely.

> and how to handle type testing.
>
> I will only be implementing the gsl functions that work with Gambas
> types i.e. integer, singles, and doubles. Is it necessary to test for
> these types in my code or will gb take care of the testing?
> If i do need to test for input types how can I tell gb that a type error
> has occurred?

As soon as you do not deal with Variants, you don't have to check types 
at all. Gambas does it for you.

But if you use object references, you should use the GB.CheckObject() 
function on them. This function returns TRUE if the object reference is 
NULL or "invalid". FALSE otherwise.

>
> The gsl library allocates memory for structures (gsl_complex, vectors,
> and matrices) internally. I'm not sure that gb is can manage this
> memory. Therefore is it best to copy the gsl allocated structures to my
> own and then free the memory allocated by libgsl? If so should I be
> using GB.Alloc for this?

Oops. No.

When you have some kinds of objects in a library (usually something 
allocated with a dedicated function, and freed or unreferenced with 
another one), then you have two cases:

1) You have full control of the library object life.

You have a pointer to this object in the Gambas object structure. You 
allocate the library object in the Gambas constructor, and free (or 
dereference) it in the Gambas destructor.

2) The library object can be destroyed independently of you.

For example: a GUI window can be destroyed by the user.

Then you must have a mechanism somewhere that will warn you. Once 
warned, you then make the Gambas object "invalid".

As I don't think that you need that at the moment, I will give more 
details only if you request them.

Regards,

-- 
Benoît Minisini




More information about the Devel mailing list