Module variable set in one module subrountine resets to 0 in another module's subroutine

Module variable set in one module subrountine resets to 0 in another module's subroutine

I have a module that I am using to share variables across a program, and I have several modules that need to access that variable. I have one integer variable that is acting unexpectedly. I have a large subroutine that itself is not a module I'll call it subroutineA. subroutineA uses the shared variables module and the variable in question, but does not set it. subroutineA calls subroutineB that sets the variable in question to a value - in my case, 1761. subroutineB returns to subroutineA and all is well with the variable in question as it is still 1761.

subroutineA does some other things, then gets to subroutineC which is contained in another module that also uses the shared variables module. If I put a breakpoint at subroutineC, I can see in the watch window that the value of the variable in question still has 1761 as its value. As soon as I step into subroutineC, the variable in question gets reset to 0, and bad things happen in subroutineC. If, in the debugger, I simply step into subroutineC, then move the execution pointer to the return statement at the end of subroutineC, then step out of subroutineC, the value of the variable in question returns to what it should be - 1761.

I've been through several steps already to try to find an answer to this:

  1. At first, the variable was passed to subroutineC as a parameter and subroutineC used the same name as in the shared variable module - however - the compiler issued no errors or warnings about this. When I first noticed the problem, I changed the name of the variable in subroutineC. Same results.
  2. In subroutineC, I tried declaring the renamed variable as INTENT(IN), same results.
  3. Thinking that there may be some sort of illegal array access (even though I have detection of array out of bounds set in the compiler), I moved the parameter to the first parameter in subroutineC's signature. Same results.
  4. I then removed the variable from subroutineC's signature, and used the access granted to it via the use statement for the shared variables module. I still get the same results.

I am doing this elsewhere, so I do not understand why it is not working in this case. It is as if the module where subroutineC is is getting another instance of the shared variables module.

Any thoughts or ideas as to what the problem is and how to solve it?

Thanks in advance!

2 帖子 / 0 全新
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项

I think I figured this out.

First, I declared a custom type that contains all of the variables that are shared. Then I used that type to declare a variable of that type. I then did a global search and replace to ensure that all the varibles that were referenced in the module are properly referenced where they are used throughout the code, i.e., SharedVars%variable_name. The value that was being reset to 0 now retains its value in subroutineC.

登陆并发表评论。