A while back, I was tasked with solving a defect in our system. The defect was “Changes to an image can be reverted only one time.”
As I started to look into the code, I saw something like this:
private Image _originalImage;
private Image _image;
public void Show(Image image) {
_image = image;
_originalImage = (Image) image.Clone();
}
private void Revert() {
_image = _originalImage;
-
Looking at the code, I suspected I knew what was wrong, but I wanted to confirm it. So I ran the app, made modifications to the image, clicked revert, made modifications to the image again, set a break point and clicked revert again. Once Visual Studio broke, I popped over to the immediate window and typed this:
&_image
Which returned: 0x1234abd0
&_originalImage
Which also returned: 0x1234abd0
Now I knew what the issue was, the original image was getting clobbered due to a missed .Clone() on line 10. A simple _image = (Image)_originalImage.Clone(); fixed the issue.