[fpc-pascal] Stack problem in externally called callback function
jonas.maebe at elis.ugent.be
Sat Aug 1 17:59:59 CEST 2009
On 31 Jul 2009, at 19:05, Wimpie Nortje wrote:
> I am trying to use a shared lib written in C in my FPC program. The
> lib creates a number of threads and requires callbacks into the main
> app. It can call the callback from any of the threads.
Calling FPC code from threads not created by FPC does not work at this
time on any unix-like platform.
> When I compile without stack checking everything (seems) to work
> fine but when stack checking is on I get stack error (error 202) on
> entry to the callback function. I can break on the callback's
> 'begin' and see valid values for all the function parameters but
> when I step into the function body it crashes.
That's because the stack checking has to be initialised per thread and
this is normally done when you create a thread via tthread.create() or
beginthread(). Disable stack checking.
> I say 'seems' when not using stack checking because when the app
> becomes large I get a lot of strange problems which would probably
> be explained by stack problems.
No, the problems are probably due to the FPC rtl not being initialised
for multi-threading. You will probably get slightly less problems if
you include the "cthreads" unit in your uses-clause and create a dummy
thread as soon as your program starts (after which you can destroy it
again), but even then you will get memory leaks and sporadic random
errors due to threadvars containing wrong values. As said above: it
does not work at this time (under unix-like platforms; in 2.3.1, it
should work on Windows).
The reason that it does not work on Unix-like platforms, is that there
is no way to make libthread inform FPC whenever a thread is created
(while this is possible on Windows), so the FPC rtl cannot initialise
the per-thread information properly. There is no known way to fix this
problem (although some people have guesses, no one really knows for
More information about the fpc-pascal