In my case, rebuilding the project sometimes solves the problem.
when rebuilding (or cleaning and then building) the project doesn't work, restarting the visual studio worked..
I think this is a compiler issue (specially in managing the heap) not the program or code.