...
In this noncompliant code example, pst
is not properly released when processItem
process_item
throws an exception, causing a resource leak:
Code Block |
---|
|
#include <new>
struct SomeType {
void processItemprocess_item() noexcept(false);
};
void f() {
SomeType *pst = new (std::nothrow) SomeType();
if (!pst) {
// Handle error
return;
}
try {
pst->processItem>process_item();
} catch (...) {
// Handle error
throw;
}
delete pst;
}
|
...
Code Block |
---|
|
#include <new>
struct SomeType {
void processItemprocess_item() noexcept(false);
};
void f() {
SomeType *pst = new (std::nothrow) SomeType();
if (!pst) {
// Handle error
return;
}
try {
pst->processItem>process_item();
} catch (...) {
// Handle error
delete pst;
throw;
}
delete pst;
} |
...
Code Block |
---|
|
struct SomeType {
void processItemprocess_item() noexcept(false);
};
void f() {
SomeType st;
try {
st.processItemprocess_item();
} catch (...) {
// Handle error
throw;
}
}
|
...
Code Block |
---|
|
#include <new>
struct A {/* ... */};
struct B {/* ... */};
class C {
A *a;
B *b;
protected:
void init() noexcept(false);
public:
C() : a(new A()), b(new B()) {
init();
}
};
|
...
Code Block |
---|
|
#include <new>
struct A {/* ... */};
struct B {/* ... */};
class C {
A *a;
B *b;
protected:
void init() noexcept(false);
public:
C() : a(nullptr), b(nullptr) {
try {
a = new A();
b = new B();
init();
} catch (...) {
delete a;
delete b;
throw;
}
}
};
|
...