Page 1 of 1
Invalid memory access on EndProcedure
Posted: Tue Aug 22, 2023 9:23 pm
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?
Re: Invalid memory access on EndProcedure
Posted: Tue Aug 22, 2023 9:39 pm
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?
Re: Invalid memory access on EndProcedure
Posted: Wed Aug 23, 2023 4:55 am
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.
Re: Invalid memory access on EndProcedure
Posted: Wed Aug 23, 2023 7:17 am
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.
Re: Invalid memory access on EndProcedure
Posted: Thu Aug 24, 2023 4:48 pm
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.
Re: Invalid memory access on EndProcedure
Posted: Thu Aug 24, 2023 5:38 pm
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
Re: Invalid memory access on EndProcedure
Posted: Thu Aug 24, 2023 7:26 pm
by Piero
Re: Invalid memory access on EndProcedure
Posted: Thu Aug 24, 2023 8:40 pm
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.
Re: Invalid memory access on EndProcedure
Posted: Sat Aug 26, 2023 4:57 pm
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.