Den 20.02.2012 21:12, skrev Sturla Molden:
>
> If you need to control the lifetime of an object, make an inner block
> with curly brackets, and declare it on top of the block. Don't call new
> and delete to control where you want it to be allocated and deallocated.
> Nothing goes on the heap unless STL puts it there.
>
Here is an example:
// bad
Foo *bar = new Foo();
<suite>
delete Foo;
// ok
{
Foo bar();
<suite>
}
Remember C++ does not allow a "finally" clause to exception handling.
You cannot do this:
try {
Foo *bar = new Foo();
} finally { // syntax error
delete Foo;
}
So...
try {
Foo *bar = new Foo();
} catch(...) {
}
// might not get here, possible
// resource leak
delete Foo;
Which is why we should always do this:
{
Foo bar();
<suite>
}
This is perhaps the most common source of errors in C++ code. If we use
C++ in the NumPy core, we need a Nazi regime against these type of
obscure errors.
Sturla
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion