FreeStructure() crash

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

FreeStructure() crash

Post by Joubarbe »

I was wondering if any of you had any crash related to the function FreeStructure().

In my app, I have a Control structure, and a list of controls associated with a view. Then I have a different structure for each gadget, and all those structures extend to Control. So it looks like something like that:

Code: Select all

Structure _Control
EndStructure

Structure _Button Extends _Control
EndStructure

Structure _Label Extends _Control
EndStructure

Structure _View
  List *controls._Control()
EndStructure

Define my_view._View

AddElement(my_view\controls())
my_view\controls() = AllocateStructure(_Button)

Define *button._Button = my_view\controls()
; Do something with the button here.

; When closing the view, I free all the associated controls.
ForEach my_view\controls()
  FreeStructure(my_view\controls())
Next
ClearList(my_view\controls())
The point of that architecture is to have a kind of polymorphism that allows me to interact with all controls at once through the list *controls._Control().

I've spent months with that code without any problem, but since a few days, I now have a crash on FreeStructure(), and this crash does not always happen, even though I do exactly the same things before the crash happens. So I know I'm throwing a message in a bottle here, but in case I missed something obvious or some people had weird problems with that function, or if you have any idea about what mistakes I potentially did, I would be happy to hear from you!

(Windows 11 64bits, PB 6.12, Purifier ON, ASM backend)
Last edited by Joubarbe on Mon Oct 14, 2024 12:18 pm, edited 3 times in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: FreeStructure() crash

Post by mk-soft »

Perhaps a double call of FreeStructure.
You should check the pointer for non-zero and set it to zero after FreeStructure.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: FreeStructure() crash

Post by Joubarbe »

The error I receive is a "write error at address [non-zero]". After freeing all structures, I ClearList() all my controls. I call FreeStructure() only once in all my code.

But I've just tried to compile on the C backend, I've tried 10 times, and there was no crash at all. Usually the 2nd or 3rd attempt crashes (if not the 1st).

EDIT: I've also just checked my session history, and compared the same files from a month ago to current ones, and I see nothing that could lead to such an error.
tored
User
User
Posts: 86
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: FreeStructure() crash

Post by tored »

Disable Purifier and test if it happens again.
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: FreeStructure() crash

Post by Joubarbe »

tored wrote: Mon Oct 14, 2024 10:00 am Disable Purifier and test if it happens again.
Unfortunately, it still crashes.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: FreeStructure() crash

Post by #NULL »

First make sure you don't actually have empty structures in the actual program. I f I run your code here, AllocateStructure returns 0 for an empty structure. While FreeStructure seem to cope with that fine, I'm not sure it's a good idea.
Second, did you try what mk-soft suggested? Or did you debug anything at all?

Code: Select all

ForEach my_view\controls()
  Debug "freeing: my_view\controls() : " + my_view\controls()
  If my_view\controls() <> 0
    FreeStructure(my_view\controls())
  EndIf
  my_view\controls() = 0
Next
ClearList(my_view\controls())
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: FreeStructure() crash

Post by Joubarbe »

Thank you #NULL. I did try what mk-soft suggested, without any good result (same code as the one you just wrote). I don't have a lot of gadgets, so AllocateStructure() is easy to track, and it never returns 0 (my code on my first post is of course just an example to better understand my program).

Debugging the control when the app crashes gives the first control of the view that is supposed to be freed. I can debug the text, the callback and everything of my button gadget, or any other.
tored
User
User
Posts: 86
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: FreeStructure() crash

Post by tored »

Probably unlikely, but both AllocateStructure() and AddElement() can fail, check that.
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: FreeStructure() crash

Post by Joubarbe »

tored wrote: Mon Oct 14, 2024 12:53 pm Probably unlikely, but AddElement() can fail, check that.
It does sometimes yes! What is the link then? It used to actually crash, then I restarted my computer (or did something else...?), and now FreeStructure() is the one that crashes all the time instead. AddElement() is in a different procedure though and doesn't crash at the same point. And same as FreeStructure(), it doesn't happen all the time, even though the steps to reproduce the crash are exactly the same (and take literally 5 seconds).
tored
User
User
Posts: 86
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: FreeStructure() crash

Post by tored »

Joubarbe wrote: Mon Oct 14, 2024 12:58 pm
tored wrote: Mon Oct 14, 2024 12:53 pm Probably unlikely, but AddElement() can fail, check that.
It does sometimes yes! What is the link then? It used to actually crash, then I restarted my computer (or did something else...?), and now FreeStructure() is the one that crashes all the time instead. AddElement() is in a different procedure though and doesn't crash at the same point. And same as FreeStructure(), it doesn't happen all the time, even though the steps to reproduce the crash are exactly the same (and take literally 5 seconds).
Do you use threads in your program?
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: FreeStructure() crash

Post by Joubarbe »

I do not use threads. Also, the crash at AddElement() does not concern the same view or the same controls (as the crash related to FreeStructure()).

(AllocateStructure never failed)
Last edited by Joubarbe on Mon Oct 14, 2024 1:11 pm, edited 1 time in total.
tored
User
User
Posts: 86
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: FreeStructure() crash

Post by tored »

tored wrote: Mon Oct 14, 2024 12:53 pm Probably unlikely, but both AllocateStructure() and AddElement() can fail, check that.
It is always important to check return values for every procedure call that has one and if fail log that somewhere and graciously abort whatever you are doing.
tored
User
User
Posts: 86
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: FreeStructure() crash

Post by tored »

Joubarbe wrote: Mon Oct 14, 2024 1:06 pm I do not use threads. Also, the crash at AddElement() does not concern the same view or the same controls (as the crash related to FreeStructure()).
You write that you use PB 6.12, which was released recently, but also that this code has worked for months. Tried compiling your program on an older PB version when it worked?
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: FreeStructure() crash

Post by Joubarbe »

tored wrote: Mon Oct 14, 2024 1:11 pm
Joubarbe wrote: Mon Oct 14, 2024 1:06 pm I do not use threads. Also, the crash at AddElement() does not concern the same view or the same controls (as the crash related to FreeStructure()).
You write that you use PB 6.12, which was released recently, but also that this code has worked for months. Tried compiling your program on an older PB version when it worked?
The crazy thing is that I cannot make the app crash when I use the C backend.
tored
User
User
Posts: 86
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: FreeStructure() crash

Post by tored »

Joubarbe wrote: Mon Oct 14, 2024 1:13 pm
tored wrote: Mon Oct 14, 2024 1:11 pm
Joubarbe wrote: Mon Oct 14, 2024 1:06 pm I do not use threads. Also, the crash at AddElement() does not concern the same view or the same controls (as the crash related to FreeStructure()).
You write that you use PB 6.12, which was released recently, but also that this code has worked for months. Tried compiling your program on an older PB version when it worked?
The crazy thing is that I cannot make the app crash when I use the C backend.
Try PB 6.11 , it is possible that this is a PB 6.12 bug. It would help Fred to pinpoint where the bug is.
Post Reply