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. Way to Prevent memeory leak?

Way to Prevent memeory leak?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorialperformance
7 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.
  • N Offline
    N Offline
    nachilau
    wrote on last edited by
    #1

    Hello all, I have a question on about how to prevent to prevent the memory leak? I am writing a window program, and I have an Assert() so that if any fatal erorr occured, the Assert() will call Sleep(INFIITE) to stop the program, and print out the error message to the user. By doing that, the only way to exit the program after Assert() has been called is to close the window. However, it causes a problem which is that even though the desturctor of each classes in the program will be called when the user close the window, there may still some memory leak in the local scoop when I use Assert() like this, eg in funciton f() f() { char* name = new char[10]; Assert(AnyError(), "Fatal Error!"); strcpy(name, "Hello"); delete [] name; } For example, if I get the Assert() if I get any error in f(), then when the user close the window, I will have a memory leak for 10 bytes. What is the best way to solve the problem? I don't want to use the try and catch block since the code will be mess up with all those try and catch block. And I really want to stop the program when the fatal error occurs. Any ideas? Thanks! Nacho

    L W 2 Replies Last reply
    0
    • N nachilau

      Hello all, I have a question on about how to prevent to prevent the memory leak? I am writing a window program, and I have an Assert() so that if any fatal erorr occured, the Assert() will call Sleep(INFIITE) to stop the program, and print out the error message to the user. By doing that, the only way to exit the program after Assert() has been called is to close the window. However, it causes a problem which is that even though the desturctor of each classes in the program will be called when the user close the window, there may still some memory leak in the local scoop when I use Assert() like this, eg in funciton f() f() { char* name = new char[10]; Assert(AnyError(), "Fatal Error!"); strcpy(name, "Hello"); delete [] name; } For example, if I get the Assert() if I get any error in f(), then when the user close the window, I will have a memory leak for 10 bytes. What is the best way to solve the problem? I don't want to use the try and catch block since the code will be mess up with all those try and catch block. And I really want to stop the program when the fatal error occurs. Any ideas? Thanks! Nacho

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Im not sure i fully understand. If you program has hit a fatal error and is exiting then you could probably not worry and rely on the OS to clean up after you. Perhaps you could use a smart pointer A smart pointer is usually a template class that pretends to be a pointer by providing the -> and * operators. Because the smart pointer itself is on the stack its destructor will be called and you can delete the memory then. I think the stl has a smart ptr template. You might want to start there However if your Assert() function never returns then that wont work either. Why dont you throw an exception in your Assert() function and catch it at a higher level Good luck

      N 1 Reply Last reply
      0
      • L Lost User

        Im not sure i fully understand. If you program has hit a fatal error and is exiting then you could probably not worry and rely on the OS to clean up after you. Perhaps you could use a smart pointer A smart pointer is usually a template class that pretends to be a pointer by providing the -> and * operators. Because the smart pointer itself is on the stack its destructor will be called and you can delete the memory then. I think the stl has a smart ptr template. You might want to start there However if your Assert() function never returns then that wont work either. Why dont you throw an exception in your Assert() function and catch it at a higher level Good luck

        N Offline
        N Offline
        nachilau
        wrote on last edited by
        #3

        Thanks for your suggestion. I know the way the smart pointer works, but as what you said, since the Assert function never return, smart pointer is no use in this case. Also, if I throw an exception, then, the try and catch block will go around in my code. That is so messy then! But thanks for your comment!!

        L 1 Reply Last reply
        0
        • N nachilau

          Thanks for your suggestion. I know the way the smart pointer works, but as what you said, since the Assert function never return, smart pointer is no use in this case. Also, if I throw an exception, then, the try and catch block will go around in my code. That is so messy then! But thanks for your comment!!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          As you are trying to deal with a rather general error could you not catch the exception once at a higher level? In your main() for example. If you are writing an MFC app im sure you could override CWinApp::Run() and put your try..catch in there.

          1 Reply Last reply
          0
          • N nachilau

            Hello all, I have a question on about how to prevent to prevent the memory leak? I am writing a window program, and I have an Assert() so that if any fatal erorr occured, the Assert() will call Sleep(INFIITE) to stop the program, and print out the error message to the user. By doing that, the only way to exit the program after Assert() has been called is to close the window. However, it causes a problem which is that even though the desturctor of each classes in the program will be called when the user close the window, there may still some memory leak in the local scoop when I use Assert() like this, eg in funciton f() f() { char* name = new char[10]; Assert(AnyError(), "Fatal Error!"); strcpy(name, "Hello"); delete [] name; } For example, if I get the Assert() if I get any error in f(), then when the user close the window, I will have a memory leak for 10 bytes. What is the best way to solve the problem? I don't want to use the try and catch block since the code will be mess up with all those try and catch block. And I really want to stop the program when the fatal error occurs. Any ideas? Thanks! Nacho

            W Offline
            W Offline
            wb
            wrote on last edited by
            #5

            if you run on something else than Win 3.11 the OS cleans up after you...

            N 1 Reply Last reply
            0
            • W wb

              if you run on something else than Win 3.11 the OS cleans up after you...

              N Offline
              N Offline
              nachilau
              wrote on last edited by
              #6

              Thanks! But do you know how the OS clean is up for me? You mean the OS will keep doing a background search for the memory leak? Am I right? Thanks!

              W 1 Reply Last reply
              0
              • N nachilau

                Thanks! But do you know how the OS clean is up for me? You mean the OS will keep doing a background search for the memory leak? Am I right? Thanks!

                W Offline
                W Offline
                wb
                wrote on last edited by
                #7

                the OS has a table with all the memory you alocated, and when your program exits , the OS throws away that table and the Memory is free for other programs...

                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