Page 1 of 1

Modules first initialize at the first use

Posted: Sat Mar 25, 2017 12:21 pm
by mk-soft
Modules first initialize at the first use or a new command InitModule for dll's

When starting an application or loading a dll, the code is always executed in the module.
This is not always good. Had problems when using modules in dll's with windows 10 x64 as COM-DLL at the registration.

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 7:05 am
by Little John
mk-soft wrote:When starting an application or loading a dll, the code is always executed in the module.
That's not quite right -- it depends on the code. If all executable code is inside of procedures, nothing will be executed automatically
(except from those 4 special procedures in case of a DLL).

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 11:36 am
by mk-soft
I do not mean that.
Of course, procedure is not called automatically.
But the code in the module.

Code: Select all

DeclareModule foo
  Global var
EndDeclareModule

Module foo ; This is running before DLL AttachProcess
  ; this can crash under Windows 10 as X64-DLL
  var = Random(100)
  ; this work stable
  Procedure do()
    var = Random(100)
  EndProcedure : do()
EndModule
Logfile from ClassDebug. Link http://www.purebasic.fr/english/viewtop ... 12&t=68101
Start Logging: 2017-03-25 17:17:22
[ClassValues.] Begin Initalize Module
[ClassValues.] End Initalize Module
[COM.DLL] AttachProzess
[COM.DLL] GetClassObject: Object ok
[ClassFactory.AddRef] RefCounterer: 1
[ClassFactory.AddRef] RefCounterer: 2
...

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 11:44 am
by Little John
mk-soft wrote:I do not mean that.
Of course, procedure is not called automatically.
But the code in the module.

Code: Select all

DeclareModule foo
  Global var
EndDeclareModule

Module foo ; This is running before DLL AttachProcess
  ; this can crash under Windows 10 as X64-DLL
  var = Random(100)
  ; this work stable
  Procedure do()
    var = Random(100)
  EndProcedure : do()
EndModule
You didn't understand what I wrote:
Any code in the module is executed automatically, only if it is outside of a procedure.
That means: Just put all your executable module code inside procedures, and then it will not be executed automatically (except from those 4 special procedures in case of a DLL)..

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 12:01 pm
by GPI
wait - the compiler doesn't report an error, when executeable programcode if outside procedures, when it create a DLLs?
If yes - this should be changed...

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 12:11 pm
by mk-soft
That means: Just put all your executable module code inside procedures
I'm aware.
However, macros automatically create an initalization procedure, which must also be called.

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 12:19 pm
by Little John
BTW: PB documentation writes
When writing a DLL, all the code is done inside procedures.
Obviously this is a general rule, which is not specific for modules.
GPI wrote:wait - the compiler doesn't report an error, when executeable programcode if outside procedures, when it create a DLLs?
If yes - this should be changed...
I agree.

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 12:23 pm
by mk-soft
GPI wrote:wait - the compiler doesn't report an error, when executeable programcode if outside procedures, when it create a DLLs?
If yes - this should be changed...
But so it still works
I like this

Code: Select all

ProcedureDLL AttachProcess(Instanz)
  ; with new command
  InitModule MyModule
  ; with first calling of
  UseModule MyModule
EndProcedure

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 12:43 pm
by Little John
mk-soft wrote: I like this

Code: Select all

ProcedureDLL AttachProcess(Instanz)
  ; with new command
  InitModule MyModule
  ; with first calling of
  UseModule MyModule
EndProcedure
You can do this already pretty easily:
In your module, create a procedure say named Init() that contains the necessary code.
Then call this procedure for initialising the module.

Re: Modules first initialize at the first use

Posted: Sun Mar 26, 2017 1:00 pm
by mk-soft
Little John wrote: You can do this already pretty easily:
In your module, create a procedure say named Init() that contains the necessary code.
Then call this procedure for initialising the module.
I know,
but please show my Module BaseClass ClassDispatch. It´s not so easy with macros...
One way is to add all initialize-procedures into a linked list. but use code outside procedures too.

Re: Modules first initialize at the first use

Posted: Wed Aug 19, 2020 4:48 am
by Rinzwind
https://en.wikibooks.org/wiki/Pascal_Programming/Units

PB only took some 'easy parts' and could have fixed other parts (keywords to specify visibility instead of duplication, or at least offer IDE help (CTRL+SHIFT+C in Lazarus)). PB: No Initialization and Finalization, no global scope available, no overriding of standard commands possible. As in: Pascal did it almost right way back in 1977...