` Printed Icetips Article

Icetips Article



Clarion 6: Threading issues with files in methods - 1
2003-01-21 -- Alexey Solovjev
 
Hello,

> GPF when opening a file in a constructor of a class:
>
> I was able to narrow down some class problems. It seems that when you
> execute some code involving files in the constructor of a class it GPF's.

Your program is incorrect. As documentation for C60 states, FILEs and VIEWs
declared within procedure or routine local scope are treated as threaded
even if the FILE has no it in its declaration. Hence, initialization of the
local FILE instance for the thread 1 is performed during this thread
initialization. Initialization of the thread 1 is performed after
initialization of the static not-threaded data.

In your program the FILE is declared in the local scope of the class
method. Hence, it is threaded. On the other hand, the instance of that
class is static not-threaded and its constructor is fired as a part of
initialization of the static data, i.e., before instance of the FILE for
thread 1 has been initialized, really, before space for FILE instance
has been allocated. As result, call to SHARE produces GPF.

All calls to the procedure within particular thread share the instance of
the FILE declared in this procedure. Therefore it's impossible to look to
allocation of the CLASS instance, if FILE is declared in the method, to
decide is FILE must be treated as threaded or not: you can have both
threaded and not threaded instances of the same CLASS:

T      CLASS,TYPE
         ...
       END

StaticInstance    T
ThreadedInstance  T,THREAD

If you need to open a FILE within constructor of some static class instance,
declare that FILE out of constructor.

> Curious enough this doesn't happen if you set the Global project options
> Run-Time Library to Local.

This is curious enough indeed. Really, the code generated by the compiler
is smart enough to perform parts of the thread-specific initialization if
they are requested before their normal time. But not everything can be done
too early. One of things that falling into this kit is dereference of the
pointer to pipe function stored in the FILE control block generated by the
compiler. Such dereference is not required if the program is linked in the
local mode. This is a reason why your program not GPFs being linked in this
case. I'll review the compiler to find a way to rearrange initialization of
FILEs declared locally but it's need to understand that not everything can
be done just because all static initialization must be done before thread 1
initialization in all involved executables. Some (most, really) of them are
not CW's.

Alexey Solovjev

E-mail:  clarion@also.spb.ru



Printed November 21, 2024, 7:15 am
This article has been viewed/printed 35335 times.
Google search has resulted in 17 hits on this article since January 25, 2004.