<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/90946>90946</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Destructors are not called during unwinding of SEH exceptions for Windows x86 binaries with /EHa
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          momo5502
      </td>
    </tr>
</table>

<pre>
    Building sample below with cl.exe (both x64 and x86) using `cl.exe /EHa a.cpp` yields the following expected output:

```
Destructor called
Exception thrown
```

Building it with clang-cl.exe for x86 using `clang-cl.exe -m32 /EHa a.cpp` yields the following output:

```
Destructor NOT called
Exception thrown
```

As one can see, the destructor is not being called when unwinding.
Note that compiling that sample using clang-cl for x64 works completely fine. This is only an issue with x86.

Another thing worth mentioning is that taking the code from `HandleDestructorCallWithException` and putting it directly within `CatchNativeExceptions` causes the x86 binary to crash, while x64 works fine.

The sample:

```C++
#include <cstdio>


struct Destructor {
  Destructor(bool* destructorCalled) : mDestructorCalled(destructorCalled) {}
  ~Destructor() { *mDestructorCalled = true; }
  bool* mDestructorCalled;
};


void HandleDestructorCallWithException(bool* destructorCalled)
{
 Destructor x(destructorCalled);
  *reinterpret_cast<int*>(1) = 1;
}


void CatchNativeExceptions(bool* destructorCalled, bool* exceptionThrown)
{
  try {
 HandleDestructorCallWithException(destructorCalled);
  }
  catch(...) {
    *exceptionThrown = true;
  }
}


int main()
{
 bool destructorCalled = false;
  bool exceptionThrown = false;

 CatchNativeExceptions(&destructorCalled, &exceptionThrown);

 puts(destructorCalled ? "Destructor called" : "Destructor NOT called");
  puts(exceptionThrown ? "Exception thrown" : "Exception NOT thrown");

  return destructorCalled && exceptionThrown ? 0 : 1;
}

```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyclkuP4jgQxz-NuZQ6Cs4DOHCgodGcZ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