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 Leak Help

Memory Leak Help

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelpquestion
7 Posts 5 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.
  • O Offline
    O Offline
    OrangeV
    wrote on last edited by
    #1

    I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.

    _ S M P 4 Replies Last reply
    0
    • O OrangeV

      I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      You have to delete the previously created object before creating a new object. If you have used new or new[] inside CreateObject you have to call delete or delete[] respectively. If malloc is used, you have to call free. If it is a COM object, you have to call its Release method.

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • O OrangeV

        I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        It all depends on what CreateObject actually does... If it does something like this:

        struct Object
        {
        whatever
        };

        Object CreateObject() { return Object(); }

        then you don't need to do anything to myObject before reassigning to it. If it uses a resource allocating function (e.g. new and malloc allocate memory, CreateFile, CreateEvent and CreateProcess allocate kernel handles, there are many more different examples of resource allocators) and passes control of that resource back to you, then you are responsible for deallocating that resource. To show a simple example:

        Object* CreateObject() { return new Object(); }

        To reassign to myObject, you'd need to deallocate the thing referenced by myObject using delete myObject

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        1 Reply Last reply
        0
        • O OrangeV

          I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          just to clarify : does CreateObject() return a pointer to an object or a reference ?

          This signature was proudly tested on animals.

          O 1 Reply Last reply
          0
          • M Maximilien

            just to clarify : does CreateObject() return a pointer to an object or a reference ?

            This signature was proudly tested on animals.

            O Offline
            O Offline
            OrangeV
            wrote on last edited by
            #5

            It's just simply: {return new Object();}

            S 1 Reply Last reply
            0
            • O OrangeV

              It's just simply: {return new Object();}

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              In which case a delete myObject; is required to deallocate the object before assigning a different value to myObject - so long as the reference held by myObject hasn't been copied to some other variable.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              1 Reply Last reply
              0
              • O OrangeV

                I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.

                P Offline
                P Offline
                Pavan_Putra
                wrote on last edited by
                #7

                No ,this will not overwrite the Memory Space. if you have created object and collected the Reference it's just a reference (i.e. constant pointer).If you will assign it a new address it 'll not delete old object.(As it should not, Bcoz that object may be used ant where else).So if you are sure it is not usable now.Then call delete on it first .Then create new and assign value to it. But if you are not assigning new value , set it to null ('ll save from Dangling pointer). So you can say if( myObject != NULL) delete myObject; myObject = CreateObject(); //or myObject = NULL;

                It's not enough to be the best, when you have capability to be great....

                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