The cause of the problem isn't necessarily where the crash is happening.
To find the problem you might want to use a debugger.
You can of course post the complete code here but if it's a big program you should probably try removing everything that is not necessary for the crash to happen first. In the process of doing so you are likely to find the problem yourself. If you still haven't found the problem at least you have made it easier for us to help you. Just make sure the program still compiles and crashes before posting the code.
Turns out the layer two was saved given by parameter (not as pointer) and layer 1 saved the pointer to the (temporary) parameter. That is why locating it later was an issue.