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 when return char* from DLL

Memory leak when return char* from DLL

Scheduled Pinned Locked Moved C / C++ / MFC
22 Posts 7 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.
  • R Rolf Kristensen

    If the application is being build with the runtime libary (CRT) as a DLL (Recommended by Microsoft), then it should be possible to delete memory in the application that have been allocated by the DLL.

    D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #13

    Correct. Based on the original question and replies, it was my impression that different heaps/CRTs are being used (see static linkage post), that's why I suggested this way. Of course, using only one CRT will solve the problem, too.


    Too many passwords to remember? Try KeePass Password Safe!

    S R 2 Replies Last reply
    0
    • D Dominik Reichl

      Correct. Based on the original question and replies, it was my impression that different heaps/CRTs are being used (see static linkage post), that's why I suggested this way. Of course, using only one CRT will solve the problem, too.


      Too many passwords to remember? Try KeePass Password Safe!

      S Offline
      S Offline
      Shivanand Gupta
      wrote on last edited by
      #14

      I m unable to use CRT can u give some kind of code

      R 1 Reply Last reply
      0
      • D Dominik Reichl

        Correct. Based on the original question and replies, it was my impression that different heaps/CRTs are being used (see static linkage post), that's why I suggested this way. Of course, using only one CRT will solve the problem, too.


        Too many passwords to remember? Try KeePass Password Safe!

        R Offline
        R Offline
        Rolf Kristensen
        wrote on last edited by
        #15

        Thank you for confirming this. I'm in a situation myself where I'm considering to convert static-libraries into DLL's to improve linker times. The entire application is already using runtime library as DLL, so the conversion should be a matter of exporting all needed classes in the static libraries.

        1 Reply Last reply
        0
        • S Shivanand Gupta

          I m unable to use CRT can u give some kind of code

          R Offline
          R Offline
          Rolf Kristensen
          wrote on last edited by
          #16

          Just follow what Dominik Reichl said in the first comment about ensuring that memory allocated in the DLL is released by the DLL again. Return the char-pointer to the DLL and let the DLL release it again.

          S 1 Reply Last reply
          0
          • R Rolf Kristensen

            Just follow what Dominik Reichl said in the first comment about ensuring that memory allocated in the DLL is released by the DLL again. Return the char-pointer to the DLL and let the DLL release it again.

            S Offline
            S Offline
            Shivanand Gupta
            wrote on last edited by
            #17

            return data char* is locally defined in DLL. so how can release memory of that variable after returning char* variable

            R 1 Reply Last reply
            0
            • S Shivanand Gupta

              return data char* is locally defined in DLL. so how can release memory of that variable after returning char* variable

              R Offline
              R Offline
              Rolf Kristensen
              wrote on last edited by
              #18

              When returning the char* then you take a copy of the char*, but they both point to the same memory in the heap of the DLL. Just pass the copy of the char* from the application to the DLL again, and let the DLL release the memory in its heap. You might consider returning a CString instead of a char*, and ensure that when the CString moves from DLL boundary to the Application boundary that a copy is made of the CString.

              S 1 Reply Last reply
              0
              • S Shivanand Gupta

                i m using New and Delete in calling application(MFC). but "delete []cData;" line have error runtime. if we use cData='\0' and after this "delete []cData;" then memory leak still continue.

                N Offline
                N Offline
                Niklas L
                wrote on last edited by
                #19

                shivanandgupta wrote:

                if we use cData='\0' and after this "delete []cData;" then memory leak still continue.

                Well, if you set cData to '\0' you are manipulating the pointer, not the data it contains. cData is set to null, and that is what you try to delete (and it always succeeds.) Doesn't the DLL supply an API for freeing this string? IMHO dll's should always be designed that way.

                home

                S 1 Reply Last reply
                0
                • R Rolf Kristensen

                  When returning the char* then you take a copy of the char*, but they both point to the same memory in the heap of the DLL. Just pass the copy of the char* from the application to the DLL again, and let the DLL release the memory in its heap. You might consider returning a CString instead of a char*, and ensure that when the CString moves from DLL boundary to the Application boundary that a copy is made of the CString.

                  S Offline
                  S Offline
                  Shivanand Gupta
                  wrote on last edited by
                  #20

                  I have try but it is not working. return char* is defined in dll's function as a local variable. it will work well when we defined static variable. but i don't want to create static variable in my DLL.

                  1 Reply Last reply
                  0
                  • N Niklas L

                    shivanandgupta wrote:

                    if we use cData='\0' and after this "delete []cData;" then memory leak still continue.

                    Well, if you set cData to '\0' you are manipulating the pointer, not the data it contains. cData is set to null, and that is what you try to delete (and it always succeeds.) Doesn't the DLL supply an API for freeing this string? IMHO dll's should always be designed that way.

                    home

                    S Offline
                    S Offline
                    Shivanand Gupta
                    wrote on last edited by
                    #21

                    if we use delete without using '\0' then problem occur about heap at run time.

                    N 1 Reply Last reply
                    0
                    • S Shivanand Gupta

                      if we use delete without using '\0' then problem occur about heap at run time.

                      N Offline
                      N Offline
                      Niklas L
                      wrote on last edited by
                      #22

                      Well, setting it to '\0' is the same as setting it to NULL. In either case, it makes the delete call a no-op, and that's why you don't get a crash, but you DO get a memory leak.

                      home

                      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