Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Coding style conformance

...

In this noncompliant code example, pst is not properly released when processItem process_item throws an exception, causing a resource leak:

Code Block
bgColor#FFcccc
langcpp
#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
bgColor#ccccff
langcpp
#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
bgColor#ccccff
langcpp
struct SomeType {
  void processItemprocess_item() noexcept(false);
};

void f() {
  SomeType st;
  try {
    st.processItemprocess_item();
  } catch (...) {
    // Handle error
    throw;
  }
}

...

Code Block
bgColor#FFcccc
langcpp
#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
bgColor#ccccff
langcpp
#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;
    }
  }
};

...