Cannot save level after dynamically assigning the materials in C++

Anyone found a solution for this? I also ran into this problem