I have a general question regarding a variable I'm trying to maintain a value for that is changing ambiguously throughout my program.
Here's a brief outline of how my program is doing the assignment/retrieval:
I set the value using MyClass::setMyVal(setThis); and later I retrieve the value using MyClass::getMyVal();.
static LPCTSTR myVal;
static void setMyVal(CString setVal)
myVal = setVal;
static LPCTSTR getMyVal()
Even further in my program, I want to access the value again and do so in the same way (getMyVal()). However, the value is no longer the same. There have been no other calls to the setMyVal method, so I'm wondering if I have a memory issue and the memory location containing this value is being overwritten with a new value?
I can't really post all the code I'm using for confidentiality reasons, but I was hoping someone might be able to recognize a misstep in my pattern. I have been able to identify where the value changes, but it's a completely arbitrary location with no reference to anything in this class, so I don't understand why it's changing.
If you need any more info, let me know. And thanks for reading through this, if anything. =]
Compiler: Microsoft Visual Studio 6.0
O/S: Windows XP
I overcame the issue simply by calling getMyVal and assigning it to a local variable at the beginning of every function that requires it, then before the function exits simply re-assigning this local value to setMyVal. Obviously this is a cheap method and still leaves the main issue unaddressed (i.e. why it's doing this). Before I did so, each successive function that called getMyVal arbitrarily changed the value of the variable in MyClass to a value that that function had (at seemingly random, although I noticed it was one of the first values that the functions worked with it all cases).
Edit2: The above solution worked because I kept the volatile LPCTSTR pointing to a CString object that I kept re-initializing in memory. While it worked okay, it wasn't a very efficient way of maintaing the value I wanted and wasn't guaranteed to work (since any function I didn't call it in that wrote over that memory location would screw up the LPCTSTR value).
> I overcame the issue simply by calling getMyVal and assigning it to a local variable at the beginning of every function that requires it
Well you could post an actual program which shows the problem, nevermind a snippet which won't even compile.
Why is the data member static anyway?
Like there is only ONE instance of that variable no matter how many instances of the class you create/destroy.
I found the issue I had was in using a LPCTSTR style null-terminated string pointer which is volatile (temporary). See MSDN index entry for more info.
I think because it was a pointer to a CString I was assigning in my functions, as soon as this CString was destroyed in the function and its memory location overwritten, the LPCTSTR no longer pointed to the correct string data. I wanted the null-terminated pointer type, so to fix this I used the LPCTSTR type locally (where required) as LPCTSTR myVar = MyClass::getMyVal(); and CString as the class parameter type and it seems to have a fixed the problem.
Note: I wrote my MyClass above wrong since I was in a rush. I'll fix it up for anybody who might come across this topic later with a similar issue.