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