How to free memory in try-catch blocks?

邮差的信 提交于 2019-12-20 09:29:38

问题


I have a simple question hopefully - how does one free memory which was allocated in the try block when the exception occurs? Consider the following code:

try
 {
  char *heap = new char [50];
        //let exception occur here
  delete[] heap;
 }
 catch (...)
 {
  cout << "Error, leaving function now";
  //delete[] heap; doesn't work of course, heap is unknown to compiler
  return 1;
 }

How can I free memory after the heap was allocated and exception occurred before calling delete[] heap? Is there a rule not to allocate memory on heap in these try .. catch blocks?


回答1:


Study the RAII idiom (Resource Acquisition Is Initialization)! See e.g. the Wikipedia article on RAII.

RAII is just the general idea. It is employed e.g. in the C++ standard library's std::unique_ptr or std::shared_ptr template classes.


Very brief explanation of the RAII idiom:

Basically, it is the C++ version of try..finally blocks found in some other languages. The RAII idiom is arguably more flexible.

It works like this:

  • You write a wrapper class around your resource (e.g. memory). The destructor is responsible for freeing the resource.

  • You create, as a local (automatic) variable, an instance of your wrapper class in a scope. Once program execution leaves that scope, the object's destructor will be called, thereby releasing the resource (e.g. memory).

The important point is that it doesn't matter how the scope is exited. Even if an exception is thrown, the scope is still exited and the wrapper object's destructor is still called.


Very crude example:

// BEWARE: this is NOT a good implementation at all, but is supposed to
// give you a general idea of how RAII is supposed to work:
template <typename T>
class wrapper_around
{
  public:
    wrapper_around(T value)
        : _value(value)
    { }
    T operator *()
    {
        return _value;
    }
    virtual ~wrapper_around()
    {
        delete _value;  // <-- NOTE: this is incorrect in this particular case;
                        // if T is an array type, delete[] ought to be used
    }
  private:
    T _value;
};
// ...

{
    wrapper_around<char*> heap( new char[50] );
    // ... do something ...

    // no matter how the { } scope in which heap is defined is exited,
    // if heap has a destructor, it will get called when the scope is left.
    // Therefore, delegate the responsibility of managing your allocated
    // memory to the `wrapper_around` template class.
    // there are already existing implementations that are much better
    // than the above, e.g. `std::unique_ptr` and `std::shared_ptr`!
}



回答2:


OK mister Java programmer:

try
{
    // Exception safe dynamic allocation of a block of memory.
    std::vector<char>  heap(50);

    // DO STUFF

    // Note in C++ we use stack based objects and their constructor/destructor
    // TO give a deterministic cleanup, even in the presence of exceptions.
    //
    // Look up RAII (bad name for a fantastic concept).
}
catch (...)
{
    cout << "Error, leaving function now";
    return 1;  // Though why you want to return when you have not fixed the exception is
               // slightly strange. Did you want to rethrow?
}



回答3:


The general answer is use RAII.

However, its possible to solve it by moving the variable out of the try{} scope:

char * heap = NULL;
try {
  heap = new char [50];
  ... stuff ...
} catch (...) {
  if (heap) {
    delete[] heap;
    heap = NULL;
  }
  ... however you want to handle the exception: rethrow, return, etc ...
}

Please note that I'm not recommending this as a good practice - but more of a down & dirty to be used only if you really know the risks and are still willing to take them. Personally, I'd use RAII.

Peace




回答4:


Either move the new before the try, so that the pointer is still in scope, or use a smart pointer like shared_ptr or unique_ptr (in a pinch, auto_ptr, but it has issues) that will clean up for you on exit. Exceptions are a huge reason why smart pointers are important.




回答5:


The 'correct' answer is RAII and shared_ptr as mentioned above, but just to be complete: in your example, you could substitute

char *heap = new char [50];

with

char *stack = static_cast<char*>( alloca(50) );

alloca is almost identical to malloc, except that it alocs memory on the stack instead of the heap, so no matter how you function exits (throwing or now), the memory will be reclaimed, and no deletes or frees are necessary.




回答6:


I have to agree with all those that said RAII, however, I'd use Boost's shared_array instead of an auto_ptr. Auto pointer calls delete and not 'delete []' which will cause leaks with an array.




回答7:


The easiest way would be to declare the variable before the try block, and then just do the initialization within the block.




回答8:


Yes - if you are considering the simplicity - pointer that is outer to your try block is the solution.

Regards




回答9:


Agreed with the answers on RAII and smart pointers.

However, if you insist, you can do this:

try { dangerous operations } 
catch { cleanup; throw; }


来源:https://stackoverflow.com/questions/3048377/how-to-free-memory-in-try-catch-blocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!