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

    <tr>
        <th>Summary</th>
        <td>
            Memory error with newPassmanager
        </td>
    </tr>

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

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

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

<pre>
    A memory error was encountered executing np in the main function.
Below is my code, I am sure that the pass in the runTimer function is being executed properly, but on return to the main function is a memory error. And the memory error is different for different IR files (sometimes `malloc(): unsorted double linked list corrupted`, sometimes `Segmentation fault (core dumped)`).
```
extern "C" void runTimer(llvm::Module &M){

 llvm::legacy::PassManager lpm;
 lpm.add(llvm::createLowerSwitchPass());
    lpm.run(M);

 PassBuilder PB;

    FunctionPassManager FPM;
    ModulePassManager MPM;
    
    ModuleAnalysisManager MAM;
    FunctionAnalysisManager FAM;
    CGSCCAnalysisManager CGAM;
    LoopAnalysisManager LAM;


    // Register analysis passes with the managers
 PB.registerModuleAnalyses(MAM);
    PB.registerFunctionAnalyses(FAM);
 PB.registerCGSCCAnalyses(CGAM);
 PB.registerLoopAnalyses(LAM);

    PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);

    FPM.addPass(timer::LowerSelect()); 
 FPM.addPass(timer::BasicBlockTimer());
 MPM.addPass(llvm::createModuleToFunctionPassAdaptor(std::move(FPM)));
 MPM.run(M, MAM);
}


int main(int argc, char **argv) {

 // Initialize LLVM
    InitLLVM X(argc, argv);

 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input IR file>"), cl::init("-"));

    cl::ParseCommandLineOptions(argc, argv, "LLVM IR File Reader\n");


    LLVMContext Context;
 SMDiagnostic Err;
    std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);

    if (!M) {
 WithColor::error(errs(), argv[0]) << "Error reading IR file: " << InputFilename << "\n";
        Err.print(argv[0], errs());
 return 1;
    } 
    runTimer(*M);
    std::cout << "Timer pass finished!" << std::endl;
    return 0;
}
```

Below is the bt from my gdb output:

```
Program received signal SIGSEGV, Segmentation fault.
0x000000000000e391 in ?? ()
(gdb) bt
#0 0x000000000000e391 in ?? ()
#1  0x0000555555bc6d75 in llvm::detail::AnalysisResultModel<llvm::Module, llvm::InnerAnalysisManagerProxy<llvm::AnalysisManager<llvm::Function>, llvm::Module>, llvm::InnerAnalysisManagerProxy<llvm::AnalysisManager<llvm::Function>, llvm::Module>::Result, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator, true>::~AnalysisResultModel() ()
#2  0x00005555556ba1b5 in std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> >::operator() (
    __ptr=<optimized out>, this=<optimized out>) at /usr/include/c++/12/bits/unique_ptr.h:89
#3 std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > >::~unique_ptr (this=0x5555576de148, __in_chrg=<optimized out>) at /usr/include/c++/12/bits/unique_ptr.h:396
#4 std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >::~pair (this=0x5555576de140, __in_chrg=<optimized out>)
    at /usr/include/c++/12/bits/stl_pair.h:185
#5 std::__new_allocator<std::_List_node<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >::destroy<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > (__p=0x5555576de140, this=0x5555577da548) at /usr/include/c++/12/bits/new_allocator.h:181
#6 std::allocator_traits<std::allocator<std::_List_node<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > > >::destroy<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > (__p=0x5555576de140, __a=...) at /usr/include/c++/12/bits/alloc_traits.h:535
#7 std::__cxx11::_List_base<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >::_M_clear (this=0x5555577da548) at /usr/include/c++/12/bits/list.tcc:77
#8 std::__cxx11::_List_base<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >::~_List_base (this=0x5555577da548, __in_chrg=<optimized out>) at /usr/include/c++/12/bits/stl_list.h:575
#9 std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >::~list (this=0x5555577da548, __in_chrg=<optimized out>) at /usr/include/c++/12/bits/stl_list.h:903
#10 llvm::DenseMapBase<llvm::DenseMap<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >, llvm::DenseMapInfo<llvm::Module*, void>, llvm::detail::DenseMapPair<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > > > >, llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >, llvm::DenseMapInfo<llvm::Module*, void>, llvm::detail::DenseMapPair<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > > > >::destroyAll (this=0x7fffffffca08)
--Type <RET> for more, q to quit, c to continue without paging--
    at /usr/local/include/llvm/ADT/DenseMap.h:368
#11 llvm::DenseMap<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > >, llvm::DenseMapInfo<llvm::Module*, void>, llvm::detail::DenseMapPair<llvm::Module*, std::__cxx11::list<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > >, std::allocator<std::pair<llvm::AnalysisKey*, std::unique_ptr<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator>, std::default_delete<llvm::detail::AnalysisResultConcept<llvm::Module, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>::Invalidator> > > > > > > >::~DenseMap (this=0x7fffffffca08, __in_chrg=<optimized out>) at /usr/local/include/llvm/ADT/DenseMap.h:754
#12 llvm::AnalysisManager<llvm::Module>::~AnalysisManager (this=0x7fffffffc9f0, __in_chrg=<optimized out>) at /usr/local/include/llvm/IR/PassManager.h:620
#13 runTimer (M=...) at /home/lqs66/Desktop/model-checking-flight-control/TimeConstraints/src/timer.cpp:52
#14 0x0000555555647a5d in main (argc=<optimized out>, argv=<optimized out>) at /home/lqs66/Desktop/model-checking-flight-control/TimeConstraints/src/timer.cpp:78
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsXE1v4zjS_jXMhYghU_48-GArcSN4Y7xB0pjdm0GLZZvbFKkmqXzMYX77gtS3Ymcng21Mt1dGuhNLVUVW1VMPS7Joagw_SIAFGq_Q-OaKZvao9OL-u5lMrnaKvS2WOIFE6TcMWiuNX6jBIGOVSQsaGIZXiDPL5QHLFHOJ7RFwQrnE-0zGlis5QMENCpYrEOoFc4OTNxwrBohE-A7TBJtMA7ZHar1qSo0pzehMfuUJ6MqUU9-BGysfFRhOtUpBizdnbpdZrCTWYDMtsVXv5-IM0JY_A7yULBdseskNZny_Bw3S4r3SjXd3j3jPBRiMyMyoBCxP3JtJkFAhVIzIDJE5Cpc4k0ZpN0emsp0ALLj8BgwLbiyOldZZaoGhSeCm3jL0BIcEpKV-xnuaCevGipUGzLIkBeYGcHrzIrbuTf7j38KrBS0xIiRChOBnxVkVS0RmQjwnKFy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