Using xxxOp.erase() directly causes the opt to crash

I had a little problem writing the pass(I used xxxOp.erase().) and I spent a long time trying to figure out the problem, but to no avail.I think there are places where I didn’t get it right.Here is how I use it.

auto ops = block->getOps<xxxOp>();
for (auto op : ops) { 
    // Some processing
    ......
    ......
    i.erase();
}

I found out through the opt crash message(from llvm-symbolizer) that there was a problem in the for (auto i : inOps) { line.But after some debugging, I found out that there is no problem if you don’t use the erase here.But to be honest, I think there are places I may not be paying attention, but I really don’t know why this is happening, I hope someone can give me some advice, thanks.

This is a classical situation of iterator invalidation… Try

1 Like

Thank you, sir. :+1: