Firebird UDF lazarus / 16-Databases ---------------------------------------------------------------------- simeonb · 2016-04-09 20:59 Hello, I have a problem with "so" library with user defined function written in Free Pascal. I use it with 32 bit firebird 2.5.4 on 64 bit OpenSuse 13.2 linux kernel 3.16.6. I want to store a value in threadvar variable and use it later, but after a while the threadvar variable is zeroed! How this is possible ? What can I do to debug the issue ? My test code is: library TestUDF; uses   cthreads; threadvar   TheThreadVar: Integer; function InitThreadVar_FB(var a: Integer): Integer; begin   TheThreadVar := a;   Result := a; end; function GetThreadVar_FB(var a: Integer): Integer; begin   Result := TheThreadVar; end; function GetCurrentThreadID_FB(var a: Integer): Integer; begin   Result := Integer(GetCurrentThreadID); end; exports   InitThreadVar_FB,   GetThreadVar_FB,   GetCurrentThreadID_FB; begin   IsMultiThread := True; end. Definition of functions: DECLARE EXTERNAL FUNCTION InitThreadVar   INTEGER   RETURNS INTEGER BY VALUE   ENTRY_POINT 'InitThreadVar_FB' MODULE_NAME 'TestUDF'; DECLARE EXTERNAL FUNCTION GetThreadVar   INTEGER   RETURNS INTEGER BY VALUE   ENTRY_POINT 'GetThreadVar_FB' MODULE_NAME 'TestUDF'; DECLARE EXTERNAL FUNCTION GetCurrentThreadID   INTEGER   RETURNS INTEGER BY VALUE   ENTRY_POINT 'GetCurrentThreadID_FB' MODULE_NAME 'TestUDF'; I set the threadvar variable with SELECT InitThreadVar(123456) FROM RDB$DATABASE I read the threadvar variable with SELECT GETTHREADVAR(1), GETCURRENTTHREADID(1) FROM RDB$DATABASE At first I receive the correct value 123456. But after 1 minute, when I try to read the value again, it returns ZERO, but the value of GetCurrentThreadID is the same. Thread is NOT changed, but the value of threadvar variable is lost. How this is possible ? ---------------------------------------------------------------------- balazsszekely · 2016-04-10 07:17 I did a little experiment. You can see the result in the picture(attachment). The image is self explanatory and answer to your question: "The value of GetCurrentThreadID is the same". Apparenly, there is no mistery at all, your so is unloaded after a while. You should check firebird ducumentation, maybe there is a way to prevent udfs to be unloaded. ---------------------------------------------------------------------- simeonb · 2016-04-10 11:53 To check if the so library is unloaded I had made the following experiment: I added a global variable var   TheVar: Integer; threadvar   TheThreadVar: Integer; function InitThreadVar_FB(var a: Integer): Integer; begin   TheThreadVar := a;   TheVar := a;   Result := a; end; .... and function to read it: function GetVar_FB(var a: Integer): Integer; begin   Result := TheVar; end; with declaration: DECLARE EXTERNAL FUNCTION GetVar   INTEGER   RETURNS INTEGER BY VALUE   ENTRY_POINT 'GetVar_FB' MODULE_NAME 'TestUDF'; I have set the value with SELECT InitThreadVar(123456) FROM RDB$DATABASE To be sure that so library cannot be loaded again, I deleted the so file. Then I checked with SELECT GETTHREADVAR(1), GetVar(1), GETCURRENTTHREADID(1) FROM RDB$DATABASE First result: GETTHREADVAR   GETVAR   GETCURRENTTHREADID 123456   123456   -154993856 After a minute: GETTHREADVAR   GETVAR   GETCURRENTTHREADID 0   123456   -154993856 Only the theadvar was lost  And I think that the library is not unloaded. ---------------------------------------------------------------------- balazsszekely · 2016-04-10 14:28 Quote@simeonb Only the theadvar was lost  And I think that the library is not unloaded.Ok, then please read this: http://wiki.freepascal.org/Multith readed_Application_Tutorial#Initialization_and_Finalization Search for "threadvar". Apparently the threadvar system needs to be initialized, otherwise thredvars won't work correctly. You should give it a try. ---------------------------------------------------------------------- simeonb · 2016-04-12 14:46 I have compiled that code with CrossKylix. And the same happened. After a minute Threadvar was zeroed. So this is may be firebird issue and is not connected with Free Pascal.