Invalid memory access on EndProcedure

Mac OSX specific forum
wombats
Enthusiast
Enthusiast
Posts: 717
Joined: Thu Dec 29, 2011 5:03 pm

Invalid memory access on EndProcedure

Post by wombats »

Quite often my program crashes with "Invalid memory access" on EndProcedure for no apparent reason. It's never consistent and doesn't always happen on the same EndProcedure. It only happens on macOS. Does anyone have any idea how I can track this down and solve it?
User avatar
STARGÅTE
Addict
Addict
Posts: 2232
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Invalid memory access on EndProcedure

Post by STARGÅTE »

Sounds like a stack corruption or memory overflow.
Do you use any user includes or third party libraries?
Have you tried to enable the Purifier in the debugger tools?
Do you use threads without enabled thread safe mode?
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
wombats
Enthusiast
Enthusiast
Posts: 717
Joined: Thu Dec 29, 2011 5:03 pm

Re: Invalid memory access on EndProcedure

Post by wombats »

I do use your TabBarGadget and the GoScintilla includes. Quite often the IMA happens in the TabBarGadget code, but I'm not placing the blame on that because the PureBasic IDE uses it and that never crashes randomly. If it happens in that include, it's usually at the end of the callback procedure.

I have tried the Purifier, but I am lost on what it actually does. Does it notify you?

I have thread-safe mode enabled.
User avatar
STARGÅTE
Addict
Addict
Posts: 2232
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Invalid memory access on EndProcedure

Post by STARGÅTE »

The TabBarGadget_Callback() procedure is bind to the canvas gadget of this custom gadget.
If it crashes there, it could be an error in the window event management.
Do you actually use threads? (CreateThread)
wombats wrote: Wed Aug 23, 2023 4:55 am I have tried the Purifier, but I am lost on what it actually does. Does it notify you?
The Purifier creates a magic number behind an allocated memory or string to detect an overwrite of this area. The Purifier can prevent randomly crashes, when a routine would corrupt the memory heap zone for example.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
wombats
Enthusiast
Enthusiast
Posts: 717
Joined: Thu Dec 29, 2011 5:03 pm

Re: Invalid memory access on EndProcedure

Post by wombats »

Thank you.

I only use CreateThread once and not for anything major.

Does the Purifier alert you of problems? I've never seen it do anything, so I'm confused on how it functions. Do we have to look at the magic numbers?

The IMA is always on the actual "EndProcedure" line. I'll try the Purifier again and try to check my window management.
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Invalid memory access on EndProcedure

Post by mk-soft »

In the thread, do you use changes from Gadgets or Windows. This does not work in macOS and Linux threads. You have to pass them on to the MainScope with PostEvent.
If you use CocoaMessage in the thread, you must create your own pool, otherwise it will be destroyed by the MainScope.

Code: Select all

Procedure MyThread(*Exit.Integer)
  Protected Pool
  
  Repeat
    Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
    
    ;TODO
    
    If Pool
      CocoaMessage(0, Pool, "release")
    EndIf
    Delay(100)
  Until *Exit\i
  
EndProcedure
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
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Invalid memory access on EndProcedure

Post by Piero »

mk-soft wrote: Thu Aug 24, 2023 5:38 pmIf you use CocoaMessage in the thread, you must create your own pool, otherwise it will be destroyed by the MainScope.
WHAT??? :oops:
:P :mrgreen: :wink:
User avatar
STARGÅTE
Addict
Addict
Posts: 2232
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Invalid memory access on EndProcedure

Post by STARGÅTE »

wombats wrote: Thu Aug 24, 2023 4:48 pm Thank you.

I only use CreateThread once and not for anything major.

Does the Purifier alert you of problems? I've never seen it do anything, so I'm confused on how it functions. Do we have to look at the magic numbers?

The IMA is always on the actual "EndProcedure" line. I'll try the Purifier again and try to check my window management.
Are you use Window, Gadgets or Menu functions within this thread?

Here is an example for the Purifier:

Code: Select all

PurifierGranularity(1,1,1,1)

Define *Buffer = AllocateMemory(10) ; Alloclate a too small memory

ShowMemoryViewer(*Buffer, 20)

PokeS(*Buffer, "Hello World!", -1, #PB_Ascii) ; Write over the allocated length, gives an error with enabled Purifier

Debug "No error without Purifier"
You can see in the memory viewer the magic number "0D F0 AD 0B", reversed 0BADF00D --> "bad food".
The Purifier checks every line, if this magic number has been corrupted.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
wombats
Enthusiast
Enthusiast
Posts: 717
Joined: Thu Dec 29, 2011 5:03 pm

Re: Invalid memory access on EndProcedure

Post by wombats »

Thank you, STARGÅTE! I'm glad to see what the Purifier does now. I'll keep it turned on to hopefully catch the problem the next time.

Thanks for the tip about CocoaMessage, mk-soft! I don't use CocoaMessage, Window, Gadgets or Menu functions in the thread. I use PostEvent to do anything I need to.
Post Reply