Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory problems

Memory problems

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionperformancetutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bugDanny
    wrote on last edited by
    #1

    I posted something similar to this earlier, but I think I'm getting closer to the problem. I wrote my own class, CFAFRecord, that is a derivative of CObject. When someone presses the button, I do something similar to

    my_ObjectArray.SetSize(10);
    CFAFRecord my_Record;
    my_Record.name = m_Name;
    my_Record.address = m_Address;
    my_ObjectArray.SetAt(0, &my_Record);

    my_ObjectArray in the example is a CObArray that is a member variable of my dialog. Later, when I try to access, I try

    CFAFRecord * my_Record = *(CFAFRecord *)my_ObjectArray.GetAt(0);
    m_Name = (*my_Record).GetName();

    When I try that last line, I get an access violation, looks like a memory access error. I think I'm handling the memory wrong somewhere, maybe in the first functiion, when I use the SetAt, I pass the pointer to my_Record and my_Record is probably destoyed at the end of the function. If that's the case, how can I can I fix this? Danny The stupidity of others amazes me!

    P D 2 Replies Last reply
    0
    • B bugDanny

      I posted something similar to this earlier, but I think I'm getting closer to the problem. I wrote my own class, CFAFRecord, that is a derivative of CObject. When someone presses the button, I do something similar to

      my_ObjectArray.SetSize(10);
      CFAFRecord my_Record;
      my_Record.name = m_Name;
      my_Record.address = m_Address;
      my_ObjectArray.SetAt(0, &my_Record);

      my_ObjectArray in the example is a CObArray that is a member variable of my dialog. Later, when I try to access, I try

      CFAFRecord * my_Record = *(CFAFRecord *)my_ObjectArray.GetAt(0);
      m_Name = (*my_Record).GetName();

      When I try that last line, I get an access violation, looks like a memory access error. I think I'm handling the memory wrong somewhere, maybe in the first functiion, when I use the SetAt, I pass the pointer to my_Record and my_Record is probably destoyed at the end of the function. If that's the case, how can I can I fix this? Danny The stupidity of others amazes me!

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      Thought so, you have now posted the offending code.

      my_ObjectArray.SetSize(10);
      CFAFRecord my_Record;
      my_Record.name = m_Name;
      my_Record.address = m_Address;
      my_ObjectArray.SetAt(0, &my_Record);

      my_Record goes out of scope and is destroyed, so that the address that is stored in my_ObjectArray now points to useless memory, thus your access violations.


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

      1 Reply Last reply
      0
      • B bugDanny

        I posted something similar to this earlier, but I think I'm getting closer to the problem. I wrote my own class, CFAFRecord, that is a derivative of CObject. When someone presses the button, I do something similar to

        my_ObjectArray.SetSize(10);
        CFAFRecord my_Record;
        my_Record.name = m_Name;
        my_Record.address = m_Address;
        my_ObjectArray.SetAt(0, &my_Record);

        my_ObjectArray in the example is a CObArray that is a member variable of my dialog. Later, when I try to access, I try

        CFAFRecord * my_Record = *(CFAFRecord *)my_ObjectArray.GetAt(0);
        m_Name = (*my_Record).GetName();

        When I try that last line, I get an access violation, looks like a memory access error. I think I'm handling the memory wrong somewhere, maybe in the first functiion, when I use the SetAt, I pass the pointer to my_Record and my_Record is probably destoyed at the end of the function. If that's the case, how can I can I fix this? Danny The stupidity of others amazes me!

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        bugDanny wrote:

        CFAFRecord my_Record;

        Should be:

        CFAFRecord *my_Record = new CFAFRecord;
        ...
        my_ObjectArray.SetAt(0, my_Record);


        "Take only what you need and leave the land as you found it." - Native American Proverb

        B 1 Reply Last reply
        0
        • D David Crow

          bugDanny wrote:

          CFAFRecord my_Record;

          Should be:

          CFAFRecord *my_Record = new CFAFRecord;
          ...
          my_ObjectArray.SetAt(0, my_Record);


          "Take only what you need and leave the land as you found it." - Native American Proverb

          B Offline
          B Offline
          bugDanny
          wrote on last edited by
          #4

          Thank you. Danny The stupidity of others amazes me!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups