[fpc-pascal] Strings and Multithreading (case)
kccmp at gmx.de
Mon Sep 15 00:29:22 CEST 2008
I thought about usings Objects in Multithreaded objects and had a look at astrings.inc to get some inspiration.
I had this problem:
one global string
lets say, both threads set the string to nil. I belive there is such a case:
Thread 1 checks for S=Nil an fpc_ansistr_decr_ref (svn version, 15.9.)
Threadswitch to Thread 2
Thread 2 checks runs the whole procedure, decrements reference count to zero (only one reference (global)) and calls DisposeAnsiString
* Evil case * Memory is reallocated by someone else in Thread2 and filled with some nonsense
Threadswitch to Thread 1
Thread 1 runs the rest of fpc_ansistr_decr_ref, assume integer at S-Firstoff is >0 we will suddenly decrement an integer inside a foreign memory block
* Very evil case* Integer was =1, we even free the memory again. (with many following problems and exceptions)
I really hope i am wrong. If i am not, my current solution is to put a mutex around fpc_ansistr_decr_ref, but i know that this solution is slow.
I am trying to find a better solution.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-pascal