Initialization of static arrays

Jun 23, 2015 at 6:49am
I'm trying to write a security wrapper which checks 3 types of security. It make the security check call once only for each type. It stores the result of this check, and the fact that this check has been done, in 2 boolean variables.

So this is what is supposed to happen:
- for each license type (1,2,3) call the security function once only
- store the result of this call, and the fact that I have already called it, in 2 boolean variables
- then for every subsequent call, check if it has already been called; if it has just send back the saved result rather than calling security again

However I am getting some random issues. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
static bool Techlib_Security_Checked[3] = {false, false, false};
static bool Techlib_Security_OK     [3] = {false, false, false};

bool Check_Security_int (int type);

extern "C" _declspec(dllexport) bool Check_Security (int type)

{
    bool result;

    int ll = type-1;

    result = false;

    if(ll >= 0 && ll < 3) {
      if(!Techlib_Security_Checked[ll]) {
        Techlib_Security_Checked[ll] = true;
        if(Check_Security_int(type)) Techlib_Security_OK[ll] = true;
      }
      result = Techlib_Security_OK[ll];
    }

    return (result);
}


bool Check_Security_int (int type)

{
    bool result = true;  // just a dummy here
    return(result);
}


Questions:
1. Do the static arrays get initialized to false when the DLL is loaded?
2. Do their values persist across multiple calls to the function Check_Security?
Jun 23, 2015 at 8:09am
1. Global variables which are defined within a dll are constructed before dllmain and destructed afterwards just like ordinary programs.
2. Yes.

If the variables are solely used by Check_Security(...) consider defining them inside the function.

However you must ensure that those variables are not defined in a header. Otherwise they appear everywhere where they are included.
Jun 23, 2015 at 8:23am
Thanks. No they are not defined in header file. The code above is repeated verbatim from my code apart from the real security call.

Regards your point 1: when your say "destructed afterwards" do you mean when the DLL is unloaded?

Regards your extra point: So if I put them inside the function, their values will still persist across calls? And more importantly they will still be initialized to false when the DLL is loaded and stay as valid variables until the DLL is unloaded?
Jun 23, 2015 at 10:48am
Regards your point 1: when your say "destructed afterwards" do you mean when the DLL is unloaded?
Right before it is actually unloaded. Basically at the end of 'DLL_PROCESS_DETACH'.

So if I put them inside the function, their values will still persist across calls?
Yes.

And more importantly they will still be initialized to false when the DLL is loaded
They are initialized at the beginning of the first call of the function.

stay as valid variables until the DLL is unloaded?
Yes.
Jun 23, 2015 at 10:57am
It looks like this code is working in Debug, but not Release. Could optimization fail to save these statics?
Last edited on Jun 23, 2015 at 12:32pm
Topic archived. No new replies allowed.