Memory Issue?

This is a discussion on Memory Issue? within the Windows Programming forums, part of the Platform Specific Boards category; I have a general question regarding a variable I'm trying to maintain a value for that is changing ambiguously throughout ...

  1. #1
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26

    Memory Issue?

    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:

    Code:
    class MyClass
    {
    private
          static LPCTSTR myVal;
    
    public
          static void setMyVal(CString setVal)
          {
                myVal = setVal;
          }
          static LPCTSTR getMyVal()
          {
                return myVal;
          }
    };
    I set the value using MyClass::setMyVal(setThis); and later I retrieve the value using MyClass::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
    Using MFC


    Edit:

    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).
    Last edited by tao; 07-27-2006 at 10:23 AM. Reason: update

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help finding potential Memory issue
    By JoshNCSU22 in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 10:58 AM
  2. What's the difference?
    By Stonehambey in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 11:26 AM
  3. threads and memory issue
    By Anubhav in forum C Programming
    Replies: 6
    Last Post: 07-25-2006, 05:51 AM
  4. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 12:22 PM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 04:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21