What causes an invalid pointer operation in Delphi after a form closes?
#1
I’ve been getting this weird “invalid pointer operation” error in my Delphi app for days, but only when a specific form closes. The debugger points to a memory address, but I can’t figure out what’s being freed twice or accessed after cleanup. Has anyone else had a crash that seems totally random until you trace through the destructor sequence?
Reply
#2
I had something like that once. closing a form would crash with an invalid pointer operation and the address looked random. turned out we freed a component twice because a child control still had a reference after the form owner was gone. I turned on diagnostic memory reporting (FastMM) and it pointed to the double free and the lingering reference. the fix was to drop the extra Free and rely on the owner to free things, and to make sure no code kept a raw pointer to the object after destruction.
Reply
#3
It felt random until I started tracing destruction order with breakpoints on FormDestroy. sometimes a timer or a background thread would fire after the form was gone and touch freed memory. another time a global singleton kept a reference and tried to free it during close. in practice, I learned to check who owns what and avoid freeing twice or touching freed objects in OnClose.
Reply
#4
One concrete action: I added a few guard rails and watched the call stack when the form tore down. I replaced some Free with FreeAndNil and removed a lingering Free in a destructor that ran after the owner was gone. we measured the heap with a diagnostic build and noticed the same destructor path every time. not glamorous, but it helped locate the spot where the destructor path touched something already freed.
Reply
#5
Do you think the issue is the form sequence itself, or could it be a third party component or a timer still firing after close?
Reply


[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Forum Jump: