Demystifying C++ - Cleanup Code

From emmtrix Wiki
Jump to navigation Jump to search


The RAII (Resource Acquisition Is Initialization) idiom is pivotal in modern C++. It ensures that resources are properly released and destructors are correctly called as variables and objects go out of their scope. In C++, leveraging the RAII idiom means the cleanup code is automatically generated and inserted as needed, especially in scenarios with complex control structures like loops and branches. This idiom guarantees that in the event of premature exits from a block due to statements like return, break, continue, or via jumps with goto, the cleanup code for the respective blocks will be executed correctly, ensuring resource and exception safety.


Interestingly, the clang C++ frontend doesn't directly insert this cleanup code right before such statements. Instead, the cleanup code is generated only at one location and jumped to directly. A state variable and a switch block at the end of the cleanup code ensure that the control flow continues at the original location.

At first glance, this approach might seem inefficient as it adds additional code for the state variable and switch blocks. However, from optimization level -O1 onwards, in later compiler phases, this overhead gets eliminated by compiler optimizations like Jump Threading [1]. In this process, the cleanup code gets duplicated, and the state variable and the switch blocks can be removed, making the generated code more efficient and straightforward.

In the C implementation, as shown in the example, the state variable like cleanupDestSlot is used to determine the jump destination after executing the cleanup code. The goto mechanism is then used to jump to the relevant cleanup code and then onward to the final destination, based on the value of the state variable.

Cleanup Code
void func() {

  ClassWithDtor var1;

  for (int i = 0; i < 10; ++i) {
    ClassWithDtor var2;

    for (int j = 0; j < 10; ++j) {
      ClassWithDtor var3;

      if (i == 5) {

        continue;
      }
      if (i == 9) {

        return;
      }

      // ...







    }

    // ...







  }
  
  // ...



}

void func(void) {
  unsigned int cleanupDestSlot;
  struct ClassWithDtor var1;

  for (int i = 0; i < 10; ++i) {
    struct ClassWithDtor var2;

    for (int j = 0; j < 10; ++j) {
      struct ClassWithDtor var3;

      if (i == 5) {
        cleanupDestSlot = 1;
        goto __cleanup1;
      }
      if (i == 9) {
        cleanupDestSlot = 2;
        goto __cleanup1;
      }

      // ...

      cleanupDestSlot = 0;
    __cleanup1:
      ClassWithDtor::__ctor_complete(&var3);
      switch (cleanupDestSlot) {
        case 2: goto __cleanup3;
      }
    }

    // ...

    cleanupDestSlot = 0;
  __cleanup3:
    ClassWithDtor::__ctor_complete(&var2);
    switch (cleanupDestSlot) {
      case 2: goto __cleanup2;
    }
  }

  // ...

__cleanup2:
  ClassWithDtor::__ctor_complete(&var1);
}

This cleanup mechanism ensures that even in complex control flow scenarios, all required cleanup operations are executed, maintaining the integrity and safety of the program.