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. ATL / WTL / STL
  4. Memory leak problem.

Memory leak problem.

Scheduled Pinned Locked Moved ATL / WTL / STL
c++performancehelpquestion
3 Posts 2 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.
  • S Offline
    S Offline
    sirtimid
    wrote on last edited by
    #1

    Hi all. I have a memory leak on a C++ MFC app I'm trying to solve. In the problematc part of code there are some function calls as follows: void functionA() { functionX(new ClassConstructor()); } Since this unmanaged code, is it safe to do that without freeing the memory allocated by the new operator, or it will be released after exiting the functionA scope? Shouldn't be something like the following a good solution? void functionA() { ClassConstructor *obj = new ClassContructor(); functionX(obj); delete obj; } Thanks in advance :)

    S 1 Reply Last reply
    0
    • S sirtimid

      Hi all. I have a memory leak on a C++ MFC app I'm trying to solve. In the problematc part of code there are some function calls as follows: void functionA() { functionX(new ClassConstructor()); } Since this unmanaged code, is it safe to do that without freeing the memory allocated by the new operator, or it will be released after exiting the functionA scope? Shouldn't be something like the following a good solution? void functionA() { ClassConstructor *obj = new ClassContructor(); functionX(obj); delete obj; } Thanks in advance :)

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      The object instantiated by the new needs a matching delete. It's not clear who's responsible for deleting it from what you've told us, but someone needs to. A comment on this code you posted:

      sirtimid wrote:

      void functionA() { ClassConstructor *obj = new ClassContructor(); functionX(obj); delete obj; }

      In this case there's no reason to use the heap. Do something like this instead:

      void functionA()
      {
          ClassConstructor obj;
          functionX(&obj);
      }

      Steve

      S 1 Reply Last reply
      0
      • S Stephen Hewitt

        The object instantiated by the new needs a matching delete. It's not clear who's responsible for deleting it from what you've told us, but someone needs to. A comment on this code you posted:

        sirtimid wrote:

        void functionA() { ClassConstructor *obj = new ClassContructor(); functionX(obj); delete obj; }

        In this case there's no reason to use the heap. Do something like this instead:

        void functionA()
        {
            ClassConstructor obj;
            functionX(&obj);
        }

        Steve

        S Offline
        S Offline
        sirtimid
        wrote on last edited by
        #3

        Thanks a lot! I'll try that out :)

        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