Hard crashes with FileMappingMBS

I’m playing with FileMappingMBS to get the result of an AppleScript back to the main app. However, the helper app crashes after 20 or so times. The apps are connected with sockets. However, the result of the AppleScripts are so large that the time for the data exchange was larger than the original solution. Therefore, I wanted to use FileMappingMBS.

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000108b71000
Exception Note: EXC_CORPSE_NOTIFY

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [0]

VM Regions Near 0x108b71000:
shared memory 0000000108b62000-0000000108b71000 [ 60K] rw-/rw- SM=SHM
–>
__TEXT 0000000108b82000-0000000108baa000 [ 160K] r-x/rwx SM=COW /Users/USER/Documents/*/AS Helper App.debug.app/Contents/Frameworks/MBS_Cocoa_ABAddressbook_Plugin_19290.dylib

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_platform.dylib 0x0000000105573ec9 _platform_memmove$VARIANT$Haswell + 41
1 XojoFramework 0x0000000104f33fb4 memorySetCString + 139
2 AS Helper App.debug 0x0000000103d7d72c MemoryBlock.=CString%%oi8s + 12
3 AS Helper App.debug 0x0000000104429518 MainWindow.MainWindow.getNextObject%A1s%o<MainWindow.MainWindow>si8 + 5480 (/MainWindow:121)
4 AS Helper App.debug 0x000000010455ae69 JSONRPCemlx.Event_RequestReceived%%oob + 3497 (/JSONRPCemlx:38)
5 AS Helper App.debug 0x000000010456206e JSONRPCSocket.HandleMessage%%os + 11310 (/JSONRPCSocket:98)
6 AS Helper App.debug 0x000000010455e7dd JSONRPCSocket.Event_DataAvailable%%o + 2525 (/JSONRPCSocket:42)
7 XojoFramework 0x0000000104ea0c00 0x104e26000 + 502784

The code of the helper app:

[code]dim result(1) as String
MailIterator.init(MailboxPath, Position)
dim temp as String = MailIterator.nextObject

Dim i As Integer = r.InRange(0, 1000)
Dim j As Integer = r.InRange(0, 1000)
Dim k As Integer = r.InRange(0, 1000)

if s = nil then s = new FileMappingMBS
if s.CreateSharedMemory(str(i) + “-” + Str(j) + “-” + Str(k), temp.LenB) then
v = s.MapView(0, temp.LenB)
if v = nil then Break
else
break
end if
v.Memory.CString(0)=temp

result(0) = str(temp.LenB)
result(1) = str(i) + “-” + Str(j) + “-” + Str(k)
Return result[/code]

And the code of the main app:

[code] dim JsonResult as JSONItem = Response.Value(“params”)
if not JsonResult.IsArray then
'do error here
end if
dim theLength as string = JsonResult.Value(0)
dim theName as string = JsonResult.Value(1)

if s = nil then s = new FileMappingMBS
if s.OpenSharedMemory(theName) then
v = s.MapView(0, val(theLength))
if v = nil then break
else
MsgBox “Failed to create shared memory region.”
end if

dim m as MemoryBlock = v.Memory
if not s.DeleteSharedMemory(theName) then Break
lastResult = DefineEncoding(m.CString(0), encodings.UTF8)[/code]

Any ideas what might cause this= From the crash it looks like a memory problem. For testing I started with small amounts of data. From the documentation it looked like DeleteSharedMemor should delete the data after I was done with it.

Sierra, Xojo 2017r1. MBS from 09.03.2017.

First idea is that Cstring is only byte longer as text to mark the end.
So the memory blocks and shared memory need to be at least one byte longer for that null byte.

And now the main app crashes:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_c.dylib 0x02674d14 strlen + 52
1 XojoFramework 0x023bfba9 memoryGetCString + 167
2 Mail Archiver X.debug 0x000416d3 MemoryBlock.CString%s%oi4 + 69
3 Mail Archiver X.debug 0x0155a3da emlxIterator.JsonResponseReceived%%ooob + 2262
4 Mail Archiver X.debug 0x0155b18c Delegate.IM_Invoke%%oob + 110
5 Mail Archiver X.debug 0x0155b1eb AddHandler.Stub.6%%ob + 83

Updated code of the helper app:

[code]dim result(1) as String
MailIterator.init(MailboxPath, Position)
dim temp as String = MailIterator.nextObject

Dim i As Integer = r.InRange(0, 1000)
Dim j As Integer = r.InRange(0, 1000)
Dim k As Integer = r.InRange(0, 1000)

if temp = “” then Return result
if s = nil then s = new FileMappingMBS

if s.CreateSharedMemory(str(i) + “-” + Str(j) + “-” + Str(k), temp.LenB + 1) then '<-- +1 here
v = s.MapView(0, temp.LenB + 1) '<-- +1 here
if v = nil then Break
else
break
end if

v.Memory.CString(0)=temp

result(0) = str(temp.LenB)
result(1) = str(i) + “-” + Str(j) + “-” + Str(k)

Return result[/code]

did you peek in the memoryblock and see what content is?

or make it bigger. I think sizes are always rounded to page size, I think (4096).

The page size did the trick.

Great. Let me put a note in the help.