Archive for February, 2011

Delphi with … do Gotcha

Thursday, February 10th, 2011

I got bitten by something the other day and it had me tearing my hair trying to figure out what was going on.  An application that had been working for years starting throwing access violations.  Stepping through it with the debugger, everything looked good.  No nil pointers, nothing obviously wrong.

Finally, I realized that I was freeing and reallocating the object referenced by a with … do block while I was inside the block.  Very simplified, the code looked something like this:


with myObject do
begin
myObject.Free;
myObject := TmyObject.Create;
end;

Pretty stupid when you think about it, but frustrating all the same.