You have a scope problem. The value of your pointer, cur_state, being an argument, only exists within this function, and will be forgotten once you return from it (while any reference it was pointing to, will not). So, the delete instruction will destroy the reference the pointer is pointing to, and the new instruction will correctly create a new instance and make cur_state point to it.
However, as soon as you return from the function, the value of the cur_state pointer (which is, the address of the instance you just allocated) will be lost, and the caller will be left with the old value, which was the address of the instance you just deleted a few lines earlier. This is, in essence, because you passed the pointer by value and not by reference. The solution is to pass the pointer by reference, or pass a pointer to the pointer, or return the new pointer instead.
Basically, the thing to understand is that you did not pass cur_state to your change_state function, but only its value. Whatever pointer variable you passed as an argument to this function, will be left unchanged, a copy of it will be made, and discarded after the function returns. The unfortunate result being, that the pointer to "new State_Exit()" never made it beyond the "return" statement. As soon as the function returned, the copy holding the new, valid pointer was discarded, and you kept on going with the old value, which now points to a deleted instance.
The new State_Exit() object is there, well and alive. You just don't know where it is, and have no reference to it anywhere in your code and no way to access it beyond the function where it was created, because you lost your pointer to it after said function returned.
Try making cur_state a reference to a pointer instead, that way you will be affecting the real cur_state variable and not just a copy, and your code should work. You can also use a pointer to a pointer (so that you can now affect the inner pointer outside the function too, as well as the object, which is pointed to by the pointed pointer), but this is more C-ish and more error-prone.
Edited by Bacterius, 01 January 2013 - 10:38 PM.
The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.
- Pessimal Algorithms and Simplexity Analysis