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. Heap

Heap

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
6 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.
  • K Offline
    K Offline
    Ken Mazaika
    wrote on last edited by
    #1

    Within a class I need to have an object in the heap. Within the constructor I make a new object and I can access the data members. When I try to access them in different member functions however I get memory access errors. I call the delete in the destructor, and not before. I'm not sure what is wrong.

    D A 2 Replies Last reply
    0
    • K Ken Mazaika

      Within a class I need to have an object in the heap. Within the constructor I make a new object and I can access the data members. When I try to access them in different member functions however I get memory access errors. I call the delete in the destructor, and not before. I'm not sure what is wrong.

      D Offline
      D Offline
      Dean Goodman
      wrote on last edited by
      #2

      Could you post your code?

      K 1 Reply Last reply
      0
      • D Dean Goodman

        Could you post your code?

        K Offline
        K Offline
        Ken Mazaika
        wrote on last edited by
        #3

        The Files[^]

        D 1 Reply Last reply
        0
        • K Ken Mazaika

          The Files[^]

          D Offline
          D Offline
          Dean Goodman
          wrote on last edited by
          #4

          Here's your problem: In CParticleEngine::init(int nParticles) you have the following line: CParticle *m_pParticles = new CParticle[nParticles]; Change it to m_pParticles = new CParticle[nParticles]; and your code will work. By declaring the m_pParticles variable again in the init() function, you have made a new variable that only has scope in the init() function, and happens to be named the same as your member variable m_pParticles. Thus the member variable never gets assigned, and you (rightfully) get memory errors when you later try and delete it. --Dean

          K 1 Reply Last reply
          0
          • K Ken Mazaika

            Within a class I need to have an object in the heap. Within the constructor I make a new object and I can access the data members. When I try to access them in different member functions however I get memory access errors. I call the delete in the destructor, and not before. I'm not sure what is wrong.

            A Offline
            A Offline
            AlexO
            wrote on last edited by
            #5

            while(nCount != nParticles) should be while(nCount < nParticles) //0 based indexes in C++

            1 Reply Last reply
            0
            • D Dean Goodman

              Here's your problem: In CParticleEngine::init(int nParticles) you have the following line: CParticle *m_pParticles = new CParticle[nParticles]; Change it to m_pParticles = new CParticle[nParticles]; and your code will work. By declaring the m_pParticles variable again in the init() function, you have made a new variable that only has scope in the init() function, and happens to be named the same as your member variable m_pParticles. Thus the member variable never gets assigned, and you (rightfully) get memory errors when you later try and delete it. --Dean

              K Offline
              K Offline
              Ken Mazaika
              wrote on last edited by
              #6

              Excellent. Thanks a bunch!

              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