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. Proper function declaration to avoid a memory leak

Proper function declaration to avoid a memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
questiondebuggingperformance
5 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.
  • G Offline
    G Offline
    georgiek50
    wrote on last edited by
    #1

    I have a function which returns a char* and I am trying to avoid a memory leak. So far it is in this format: char* MyFunction(char *szBuffer) { char *szTmpBuffer = new char[strlen(szBuffer) + 1]; ...function body... return szTmpBuffer; } And it is called somewhat like this: char *szBuffer = MyFunction(szString); but if after I use szBuffer I try to delete it: delete [] szBuffer; I get a Debug Assertion failed...so the question is, what is the proper way to call new within a function then delete the pointer outside of the function w/o causing errors and avoiding memory leaks?

    D J 2 Replies Last reply
    0
    • G georgiek50

      I have a function which returns a char* and I am trying to avoid a memory leak. So far it is in this format: char* MyFunction(char *szBuffer) { char *szTmpBuffer = new char[strlen(szBuffer) + 1]; ...function body... return szTmpBuffer; } And it is called somewhat like this: char *szBuffer = MyFunction(szString); but if after I use szBuffer I try to delete it: delete [] szBuffer; I get a Debug Assertion failed...so the question is, what is the proper way to call new within a function then delete the pointer outside of the function w/o causing errors and avoiding memory leaks?

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

      You code is fine. I tried it and no assertion was fired. What line/file does the assertion message indicate?


      "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

      1 Reply Last reply
      0
      • G georgiek50

        I have a function which returns a char* and I am trying to avoid a memory leak. So far it is in this format: char* MyFunction(char *szBuffer) { char *szTmpBuffer = new char[strlen(szBuffer) + 1]; ...function body... return szTmpBuffer; } And it is called somewhat like this: char *szBuffer = MyFunction(szString); but if after I use szBuffer I try to delete it: delete [] szBuffer; I get a Debug Assertion failed...so the question is, what is the proper way to call new within a function then delete the pointer outside of the function w/o causing errors and avoiding memory leaks?

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        The handling of szTmpBuffer you show is OK. Most likely, you're overwriting the allocated space. What does the debug assertion message look like? Something about damaged blocks? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

        G 2 Replies Last reply
        0
        • J Joaquin M Lopez Munoz

          The handling of szTmpBuffer you show is OK. Most likely, you're overwriting the allocated space. What does the debug assertion message look like? Something about damaged blocks? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

          G Offline
          G Offline
          georgiek50
          wrote on last edited by
          #4

          Exactly: DAMAGE: after normal block (#3604) at (memory address)

          1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            The handling of szTmpBuffer you show is OK. Most likely, you're overwriting the allocated space. What does the debug assertion message look like? Something about damaged blocks? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

            G Offline
            G Offline
            georgiek50
            wrote on last edited by
            #5

            Yep, I was overwriting...thanks for pointing out my stupid mistakes ;P

            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