Page 1 of 1

Building a DLL - what's real code

Posted: Mon Jun 24, 2013 3:14 pm
by mariosk8s
The doc text
The DLL code is like a PureBasic code except than no real code should be written outside of procedure.
tells me that anything that "runs" should be inside of a procedure. This is as intended. I read things that are compile time such as compiler directives and variable and constant declarations are OK. Procedure calls are not.
On Mac OS X, this is a particular problem, while Linux and Windows let you get away with it. This week after a Bus Error head banging session i learned, that

Code: Select all

Global lastIdx = 4
Global Dim someArray.s(lastIdx)
is ok, but the following is not

Code: Select all

someArray.s(0) = "foo"
is not. Both look like variable declarations to me, but apparently the internal purebasic implementation is indeed a function call. I guess Maps are also at risk.

Maybe it would be useful to elaborate a bit on which things are allowed, and which not. By allowed we should focus on the strictest platform, i guess Mac. I above example should definitely be part of that.

I would also be worth mentioning that Mac OS X is really strict on this, and that it'll rain Bus Errors and Segfaults if this rule is violated, and that other platforms are a bit more permissive.

Re: Building a DLL - what's real code

Posted: Mon Jun 24, 2013 3:29 pm
by luis
I would like it too, I asked something similar three years ago -> http://www.purebasic.fr/english/viewtop ... 13&t=42537

As you can see all the threads linked there were not particular satisfying :)
mariosk8s wrote:By allowed we should focus on the strictest platform, i guess Mac.
I disagree here, if more info are given, they should be distinct by platform.
No reason to limit myself on Windows when not needed just because elsewhere something is not permitted.
Knowing all there is to know, I can then make my decisions.

Re: Building a DLL - what's real code

Posted: Mon Jun 24, 2013 3:57 pm
by Tenaja
The reason the second var declaration failed is because you are attempting to initialize it. Initializations include "real code," in this case it is the same as an assignment. If you need to init variables, either static or global, you should do them in an Init proc for the dll.

Re: Building a DLL - what's real code

Posted: Mon Jun 24, 2013 4:40 pm
by luis
Well, even lastIdx has been initialized then.
lastIdx too is initialized with "real code" (a MOV instruction).
The reason is probably the var is of type string, so memory allocation is occurring, something a lot more heavy then a MOV.

That's my guess at least.

Re: Building a DLL - what's real code

Posted: Mon Jun 24, 2013 5:54 pm
by Tenaja
luis wrote:Well, even lastIdx has been initialized then.
lastIdx too is initialized with "real code" (a MOV instruction).
The reason is probably the var is of type string, so memory allocation is occurring, something a lot more heavy then a MOV.

That's my guess at least.
I did miss that other var getting an init value, but even so, is it really getting initailized? Does any code in a dll get executed outside of a proc call? I'd be surprised, because unless Fred's OpenLibrary command forces the main file to execute the "free-form" code, then it will not happen.

Re: Building a DLL - what's real code

Posted: Mon Jun 24, 2013 8:32 pm
by luis
Tenaja wrote: I did miss that other var getting an init value, but even so, is it really getting initailized?
On Windows, yes.

Re: Building a DLL - what's real code

Posted: Fri Mar 06, 2020 9:24 pm
by Andre
This is something, which Fred should check and clarify in the Docs... 8)