Modules first initialize at the first use

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Modules first initialize at the first use

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Modules first initialize at the first use

Post 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).
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Modules first initialize at the first use

Post 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
...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Modules first initialize at the first use

Post 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)..
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Modules first initialize at the first use

Post 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...
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Modules first initialize at the first use

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Modules first initialize at the first use

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Modules first initialize at the first use

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Modules first initialize at the first use

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Modules first initialize at the first use

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Modules first initialize at the first use

Post 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...
Post Reply