strange behaviour after extracting optimization pass code


I am expieriencing strange behaviour of llvm's optimization passes and I don't understand what I am doing wrong.
Basically all I've done is extracting code for optimization of a llvm-function in a llvm-module and put it into a separate function for better readability. The original code looks like follows (and works as expected):


void optimizeFunction(std::string functionName, llvm::Module* mod) {
    llvm::Function* f = mod->getFunction(functionName);

    llvm::ExistingModuleProvider mp(mod);
    llvm::FunctionPassManager fpm(&mp);

maybe when you leave this function the above two objects are
being finalized and freeing memory?