Subject Can ib_util_malloc fail, because of low memory and what to do in this case?
Author vincent_kwinsey
Hi,

I just went through the examples, how to use ib_util_malloc in UDF,
but I didn't see any suggestions that one should check the result of
ib_util_malloc. It should be quite possible, that ib_util_malloc can
fail because of low memory condition and hence the result, which is
assigned to the pointer, is null. And when one is trying to use this
pointer, then the 'access violation' exception is raised, server
restarts and firebird logfile has records:
"he user defined function: MYFUNCTION
referencing entrypoint: fMYFUNCTION
in module: MY_UDF_DLL
caused the fatal exception: Access violation.
The code attempted to access a virtual
address without privilege to do so.
This exception will cause the Firebird server
to terminate abnormally."

How to handle this situation? One can imagine, that the best way
would be to stop the execution of UDF function and return some error
message to the user, this could prevent the crush and restart of
firebird server and so - this would not harm the ongoing transactions
of other users. But there is no way how to raise exceptions in UDF's -
at least - there is unsolved issues in FB tracker, e.g.:
http://tracker.firebirdsql.org/browse/CORE-804 and
http://tracker.firebirdsql.org/browse/CORE-742. So - what to do in
the present situation, I am using FB 1.5, but I guess, the situation
is the same for FB 2 too.

And one more question - what is the intended behavior of FB server
for select:
select MYFUNCTION(test_field) from testtable;
if the number of records in testtable is really large and MYFUNCTION
handles strings and hence - calls ib_util_malloc. From my tests I can
see, that server allocates more and more memory during the execution
of select and at one point is crushes? Well - there can be memory
consumption, because FB needs to form the answer set, but what about
deallocation of memory that is allocated in MYFUNCTION. Does it
occure after each call of MYFUNCTION, or does it occure after the
select is completed?