Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys & Gals,

This is a continuation from my previous project (See here). Thanks to much help from you, we were able to successfully rip apart the DLL into multiple modules. However, more problems came up!

Anyhow, the old code had an abundance of static variables, functions and externs, some appropriate some not so much. They all worked well when they existed as one module, but after separating them, most of them are no longer functional, and the values sometimes gets reset.

Has anyone encountered this? How did you solve them?

Thanks!
Posted
Comments
Albert Holguin 11-Feb-11 20:58pm    
I know resources within the dll are different than those within the exe and you have to change over manually, could this be a problem where a static was defined within the executable but you're trying to access it in the dll?

You say that you have separated modules that contained static variables.
This is kind of obvious, but if you have one file with
static int my_static;
and then you have "mechnically", e.g. without much thinking, partitioned code into two files, both with:
static int my_static;
then you have two "int" variables in memory instead of one, and if you initialize my_static in file #1, you are not initializing file #2 my_static variable as they are completely independent.
The net result would look, on the surface, as if (as you mentioned), static variables "resetting".
Just in case if this is the case, fix it by removing static keyword and declaring variables as extern in all but one file.
 
Share this answer
 
Only in someone's legacy code. If you did it yourself (or even if you did not) — get your hands dirty. Sorry, dirt is dirt, it cannot go without hard and unpleasant work.

However, depending on your compiler, increase in warning level to maximum can help to detect some dead code, albeit not completely.

There are different code analysis tools. Try to find some:

http://en.lmgtfy.com/?q=(find+OR+detect+OR+remove)+(unused+OR+dead)+code+automatically+%22C%2B%2B%22[^]

I don't know you platform, compiler, etc., so see the search results, there are over 10 million, top of the list looks relevant.

—SA
 
Share this answer
 
Comments
Rock Bottom 11-Feb-11 16:51pm    
Yeah, this is indeed legacy code from many years ago written by someone else. This is in Windows using Visual Studio 2008.

The main thing right now is just that, after we init an instance, next time we come back around, it becomes NULL again. We're suspecting it might have to do with the loading of different DLLs.
Sergey Alexandrovich Kryukov 11-Feb-11 17:14pm    
It does not look as anything specific. Nothing more like a regular dirt. Go to context menu and hit "Find All References" on the declaration in question or perform global search...
--SA
Rock Bottom 14-Feb-11 14:18pm    
So, would the same problem still happen with instance variables? I'm just trying to understand where the root of the problem is.
Sergey Alexandrovich Kryukov 16-Feb-11 22:03pm    
Yes, in principle, but less likely, especially if the instance's relative time is long, encapsulation is not distinct and inter-instance collaboration is a spaghetti, so logically such variables could be "almost static". It cannot happen to stack variables, of course :-)

Did you try to find any code analysis tools for far?

--SA
First off, it is very hard to say with out looking at the code. If you read your description closely, you mentioned that many of the static variables are not functional. Well, the first question you need to ask your self, do you want to keep them static? and Why? After breaking it apart, expect like SA mentioned it to get your hands dirty.

Not only do you want to break it apart you may have to question some of the functionality. May be you need to refactor some code and in some case you need to merge code/functionality.

Look for compiler warnings, they can give you some hints, and you may want to instrument your code to understand what is going on at run time. Good Luck!
 
Share this answer
 
Comments
Rock Bottom 11-Feb-11 17:19pm    
At this point, the app compiles and links, it's just that sometimes the objects or points get reset across the different scopes. A lot of the code that were statics probably shouldn't stay as statics, that's what I meant when I said they were abused.

I guess I'm just trying to understand what caused the problem so that I can start figuring out how to solve them.
Yusuf 11-Feb-11 17:24pm    
If the issue is at runtime, you may want to introduce some sort of logging. May write when and which object is accessing the variable. That may give you the clue.
Also enumerate the process how and when each DLL is loaded and accessed. Is it dynamically loaded?
Rock Bottom 14-Feb-11 14:16pm    
Dynamically.

So, would the same problem still happen with instance variables? I'm just trying to understand where the root of the problem is.
Yusuf 14-Feb-11 14:31pm    
Probably not. Remember instance variables will be different for each instance, where as static variables, well you only have one copy around. Don't you?

Now that does not mean you won't have other problems.
Rock Bottom 14-Feb-11 14:49pm    
Yeah, I was just thinking whether or not that would be a valid way to solve this, provided we can guarantee only one instance of that class. I'm was just throwing it out there as we suspect that the initialization of static variables on the load of a DLL is the culprit. Also, it's always good to clean up the code and remove any stuff that doesn't make sense (such as all these statics).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900