   [C++ Win32] Allocating and deleting a buffer
  I need to allocate a buffer that will be used in the program each time a certain message arrives.. When do I need to free it? What's the message that tells you that the user pressed the exit button of the window?Another question about allocating memory - If I allocated memory in a program, and I forgot to delete it, and I ran the program, would the memory still be in the ram after the program ends? If so, when does it free? Only when you turn off the computer?Thanks for any helpers,Portishead.   
  Usually the OS is smart enough to free any memory leaks that you leave behind. With that said, it doesn't mean that you should never delete your memory. Best way to know when you should deallocate your memory is when you leave scope or never use it again. Best practices dictate that you always deallocate what you allocate. For example:In a Function:void foo(){  // Allocate 100 bytes  char *buf = new char[100];  // Do stuff with your buffer  // Free the memory  delete [] buf;}Alternatively, if you use classes, use the destructor:class foo{  char *buf;public:  // Allocate in constructor  foo() {buf = new char[100];}    // Deallocate in Destructor  ~foo() {delete [] buf;}  // Functionality to use buffer  void doSomethingWithBuffer();};If you want to pass buffers around your code that cross scope boundaries, I suggest that you wrap the pointers up in some sort of structure, such as a reference counted auto pointer. This way it will get auto deleted when the reference reaches zero (ie. on exit). I do this in my engine and it works brilliantly.   ;  Thanks for the help. :)What I did is allocate the buffer, and when WM_CLOSE happens, before I close the window, I delete it. Will it be safe from leaks?   ;  For the most part, I believe the WM_CLOSE message is always posted to the message queue. So as long as you handle it, you should be fine. But the again, I have not seen your code and cannot say for certain what you do.   ;  It basiclly goes like this:...defines...char* buf = new char[100];...more vars......function declarations..void func () { //The function that uses the buffer }...more functions...;  Since the buffer is of a constant size, and there is no custom destructor that needs to run, there is nothing gained by using dynamic allocation.Just writechar buffer[100];and then don't do anything to deallocate it (it will be cleaned up just like any other non-array, primitive global variable).But I have to wonder: what is this buffer for? In C++, we normally use std::string to represent text, and std::vector<char> to represent chunks of unformatted data.   