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

    <tr>
        <th>Summary</th>
        <td>
            Segfault in ~OwningOpRef, op->erase()
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          pramit-j2-sl
      </td>
    </tr>
</table>

<pre>
    ```
#include "mlir/IR/OwningOpRef.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Support/FileUtilities.h"
#include "mlir/Tools/mlir-opt/MlirOptMain.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/ToolOutputFile.h"

int main(int argc, char **argv)
{
  mlir::DialectRegistry registry;

  // Register and parse command line options.
  std::string inputFilename, outputFilename;
  std::tie(inputFilename, outputFilename) =
 registerAndParseCLIOptions(argc, argv, "Read mlir file and display\n", registry);

  std::string errorMessage;
  auto output = mlir::openOutputFile(outputFilename, &errorMessage);
  if (!output)
 {
    llvm::errs() << errorMessage << "\n";
    exit(1);
 }

  llvm::SourceMgr sourceMgr;
  mlir::OwningOpRef<mlir::ModuleOp> module;
  mlir::MLIRContext context(registry, mlir::MLIRContext::Threading::DISABLED);
 mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);

  context.allowUnregisteredDialects(true);

 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
 llvm::MemoryBuffer::getFileOrSTDIN(inputFilename);
  if (std::error_code ec = fileOrErr.getError())
  {
    llvm::errs() << "Could not open input file: " << ec.message() << "\n";
    return -1;
  }

 // Parse the input mlir.
 sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), llvm::SMLoc());
 module = mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &context);
  if (!module)
  {
    llvm::errs() << "Error can't load file " << inputFilename << "\n";
    return 3;
  }

 module->dump();
  return 0;
}
```


```
// RUN: my-opt -mlir-print-debuginfo -allow-unregistered-dialect %s | FileCheck %s

module {
  // CHECK: "hello.world"
  "hello.world"() : () -> ()
}
```

Segfault location:
```
~OwningOpRef() {
 if (op)
      op->erase(); // -->here, when dtor called at the end of main()
```

Help to fix.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycV0tz4yoT_TV4Q9llIT8XXtiyU5P64vFXycx6CouWzFwEuoCSyeb-9lsg9PAjieumUjZBHLr79OlWhxrDcwmwQtMNmm4HtLInpVelpgW3w99kaMTgqNj7Cs3G4Xe8ReM1IjGXqagYYERIIbhG5OHxGZGHw5vkMj-Uz5CNToiQT87_n2oD3eLr48aErz2VNP8a4R3aVFxYLg-l-er4S1WWSltEHh64gJ-WC245fAn7oZRwjrk_h6p0-L3g-lDaPeXyS7Sm0mRKF01snxgU4rU48zNRRUEle-IS7gdp-vZLGauBFveDXlSlU9jnH3N-BXG8HCpbVtbR2cf5Ty4tLiiXiCzckuo8RSTB6YlqjMgakTXV-SsiywCab-oFxp65eI3i9ZZTAal9hpwbq9-xDgsUb_qmMEbkAZEHXB8EjalkuHSqw2lNIRZcAlal5UqaUQMzltWGjNVc5pjLEI2kBThvVRuf34k3V0jLwUf4OZAsMYq3AayDl2vJfGUkT4-H2jFEFg1PNTmJY_4ZKPOk4IwL8LExbkpB39E0kY52knTUkOUVO5dhgtZK78EYmvdjopVVwXHnbS8PqgTZpRqRxWV4zs_Z2bU9NzDmGUZkgUhU49qk417WMfYK8_ZAa-MBjrYExcmZy82eizwQEPeugT_cIrKIzlxA8-05J52xVvrYNKvefR0Jvc6H4qTb3ytWCTiUKN7hwq9vwvdPj8-Jkhb-WJzW34gsurQlt8_WGz9OGijjMg918fiy3jzttmchdvA2oi2nuVTG8vQblUx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