Firebird crashes after some time of using my UDF?


You are probably allocating memory and not freeing it. Firebird knows nothing about memory allocations in Delphi or whatever tool you might be using. If your UDF returns string result, you must always use ib_util_malloc() to allocate memory for it. The UDF must be declared as FREE_IT, so that Firebird releases the memory after it reads the string.

To use ib_util_malloc(), you need to import it from ib_util.dll into your program - and make sure you use it instead of regular memory alocating functions. Delphi example:


function ib_util_malloc(l: integer): pointer; cdecl; external 'ib_util.dll';

function ChangeMyString(const p: PChar): PChar; cdecl;
var
s: string;
begin
s := DoSomething(string(p));
Result := ib_util_malloc(Length(s) + 1);
StrPCopy(Result, s);
end;


Declaration in Firebird:

DECLARE EXTERNAL FUNCTION ChangeMyString
CString(255)
RETURNS CString(255) FREE_IT
ENTRY_POINT 'ChangeMyString' MODULE_NAME '......'


Do you find this FAQ incorrect or incomplete? Please e-mail us what needs to be changed. To ensure quality, each change is checked by our editors (and often tested on live Firebird databases), before it enters the main FAQ database. If you desire so, the changes will be credited to your name. To learn more, visit our add content page.



All contents are copyright © 2007-2024 FirebirdFAQ.org unless otherwise stated in the text.


Links   Firebird   News   FlameRobin   Powered by FB: Home Inventory   Euchre  
Add content   About  

Categories
 Newbies
 SQL
 Installation and setup
 Backup and restore
 Performance
 Security
 Connectivity and API
 HOWTOs
 Errors and error codes
 Miscellaneous