Getting Problems implementing OSX Callback Routines lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- Josh · 2016-09-10 17:43 Hi I have been banging head against wall for a long time on this one; and today i decided I would have another go. The first problem I have is that I cannot test my routines in debug mode As when I do; and a message is received and sent to my callback handler it errors at the begin section with run error 202; a stack error. Code: [Select]procedure OSX_MIDIReadProcInput( list:MIDIPacketListPtr;                    procRef:UnivPtr;                    srcRef:UniVPtr);   MWPascal; var nBytes,iByte,iSize:Integer; Status,Byte1,Byte2:Byte; i,j:integer; s:string=''; //My_Midi_Packet:MidiPacket; Begin   <----- error s here   //{$ifdef display_debug}vl3.add_debug_line('Midi In Callback');{$endif}   My_Midi_Packet:=list^.packet[0];   for i:=0 to list^.numPackets Do      After googling I found an article which suggested it was a known implementation issue on osx and to compile without the -dDebug option; If I compile in Release mode it does run and receive the the messages in the callbak; but this makes t hard to debug other things. below compiler directives and units used. Code: [Select]{$mode delphi} {$H+} {$linkframework CoreMidi} interface uses   CThreads,LCLIntf, Classes, Messages, SysUtils, Math, MacOSAll,dialogs,strutils;  I have Managed to get the routine to get the midi data; however where I am stuck is working out how to transfer this info; which i have populated into an array of bytes into a memorystream of bytes; Does lazarus have a thread safe means of converting an array of bytes into a memorystream? Hope somebody can help; so I can finally finish this routine off. ---------------------------------------------------------------------- Jonas Maebe · 2016-09-10 17:52 Add {$s-} to your source file to only disable stack checking and to keep the rest of the debug options (if set, e.g. via -ddebug). ---------------------------------------------------------------------- Josh · 2016-09-10 19:03 Hi Jonas, Thanks for your fast reply. I had tuned off the -Ct option in project option; thinking this would disable the Stack checking; but obviously it's not the same as adding the $s- option into the unit that has the problem. When I use the Directive $S-  ; it does help in that it passes the section that caused the problem; but now I am getting External Sigsev further down; which is ok in release mode; I have noticed these appearing in the event log Code: [Select]Module Load: "/System/Library/PrivateFrameworks/CoreServ icesInternal.framework/Versions/A/CoreServicesInternal". No Debug Info. Thread Group: i1 (10101) Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/BookmarkCommon.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/BookmarkData.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/CFURLAliasInteroperability.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/CFURLBookmark.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/CFURLBookmarkFile.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/CFURLBookmarkProperties.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/CFURLBookmarkResolution.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/CoreServicesInternal_vers.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/ExternalProvider.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/FileURL.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/FileURLEnumerator.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/FileURLReplaceObject.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/PlatformUtils.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/ScopedBookmarksClient.o'': can''t open to read symbols: No such file or directory.' Debug Output: '`/Library/Caches/com.apple.xbs/Binaries/CoreServicesInt ernal/CoreServicesInternal- 248.2~11/Objects/CoreServicesInternal.build/CoreServicesInternal.build /Objects-normal/i386/ftsattr.o'': can''t open to read symbols: No such file or directory.' Stack Code: [Select]#0 fpc_popaddrstack at :0 #1 ?? at :0 #2 ?? at :0 #3 __MIDIInputPortCreate_block_invoke at :0 #4 LocalMIDIReceiverList::HandleMIDIIn(unsigned long, unsigned long, void*, MIDIPacketList const*) at :0 #5 MIDIProcess::RunMIDIInThread() at :0 #6 MIDIProcess::MIDIInPortThread::Run() at :0 #7 XThread::RunHelper(void*) at :0 #8 CAPThread::Entry(CAPThread*) at :0 #9 ?? at :0 #10 ?? at :0 #11 ?? at :0 #12 ?? at :0 Assembler Code: [Select]0001C144 ffd2                     call   *%edx 0001C146 89c3                     mov    %eax,%ebx 0001C148 eb09                     jmp    0x1c153 0001C14A 8b8615304500             mov    0x453015(%esi),%eax 0001C150 8d5804                   lea    0x4(%eax),%ebx 0001C153 8b03                     mov    (%ebx),%eax 0001C155 85c0                     test   %eax,%eax 0001C157 750c                     jne    0x1c165 0001C159 b8ff000000               mov    $0xff,%eax 0001C15E e88d220000               call   0x1e3f0 0001C163 eb07                     jmp    0x1c16c 0001C165 8b03                     mov    (%ebx),%eax 0001C167 8b4004                   mov    0x4(%eax),%eax              <--------- Here 0001C16A 8903                     mov    %eax,(%ebx) 0001C16C 8b1c24                   mov    (%esp),%ebx For the moment I am using a Global String to transfer data from this thread Callback back to the main application; not ideal but should be ok as still in test. Do you think it would be safe to call a routine in my main unit from within this call back routine; this routine in my main unit does the gui stuff. Using Laz 1.4.4 and FPC 2.6.4 at the moment; I thought I should add this as it may be relevant. ---------------------------------------------------------------------- Jonas Maebe · 2016-09-11 20:43 That backtrace suggests the callback should not be a procedural variable, but a C-block (https://en.wikipedia.org/wiki/Blocks_%28C_language_extension%29 ). What is the C declaration of the routine that you use to install the callback, and how did you translate it? Support for interoperating routines that expect C-blocks as callbacks is not available in FPC 3.0.x though, and only in the current FPC development versions (http://wiki.freepascal.org/FPC_New_Features_Trun k#Support_for_interfacing_with_C_blocks_functionality ) ---------------------------------------------------------------------- Josh · 2016-09-11 21:35 Hi Jonas, The call back I am using is from MACOSALL, MIDISERVICES which is included in lazarus. Code: [Select]MIDIReadProc = procedure( (*const*) pktlist: MIDIPacketListPtr; readProcRefCon: UnivPtr; srcConnRefCon: UnivPtr );  function MIDIInputPortCreate( client: MIDIClientRef; portName: CFStringRef; readProc: MIDIReadProc; refCon: UnivPtr; var outPort: MIDIPortRef ): OSStatus; external name '_MIDIInputPortCreate'; Hope the above is what you were looking for ---------------------------------------------------------------------- Jonas Maebe · 2016-09-20 18:04 Apparently that callback gets called from a separate thread started by the MIDI framework (http://stackoverflow.com/questions/37263436/how- to-properly-use-midireadproc ). While the FPC RTL should correctly deal with externally started threads (at least in FPC 3.0, and possibly also in FPC 2.6.4 already -- but not sure about the latter), maybe the issue is related to that. You could try not to use any managed types in your callback, so no calls to the FPC exception support routines get inserted. I.e., change the "string" to "shortstring" for starters (assuming you don't need more than 255 characters). ---------------------------------------------------------------------- Josh · 2016-09-27 13:44 HI Jonas; I removed the String routines and used a global Array of Bytes to store the data arriving; and when finished transfer to stream; this however still caused odd behaviour; I then added the modeswitch blocks (fpc 3.0.1> I think) and this so far has solved the problem. ---------------------------------------------------------------------- Jonas Maebe · 2016-09-28 08:59 The blocks modeswitch by itself does not change anything. It merely enables the use of certain syntactical constructs to interface with the blocks run time. The backtrace you posted shows the use of a block, but it's probably internal to the framework. It probably calls the function pointer via a local variable of a block, but that's not something that should affect you. That said, are you perhaps using macOS 10.12 ("Sierra")? I just read a mail from someone who started to have threading problems after upgrading to that OS version. ---------------------------------------------------------------------- Josh · 2016-09-28 09:20 Hi Jonas, I am using El Capitan, I noted the Post on threads etc using Sierra so I will not be upgrading to that for a while ( not sure if the user has tried compiling without debugger and see the results; does he also get problems if he runs it from the app bundle ( so no debugger at all); I only ask as I get some problems now and again when applications are run with and without debugger ; but the run fine on their own. ( I am using Xcode 7.3.1). If I remove the mode switch; my routine stumbles at the location; by enabling it it run fines; so its an odd one; and I was just experimenting and searching so I thought I would try it, it works in my situation. On a side note; my mac done an update recently ( not the Sierra) one; and now I cannot run my application from the ide; it compiles link and says everything is ok. The ide title bar says that it is running and debugging; but the application does not start; i can click the app bundle in finder and everything is ok; this is with debug on/off. Could possible do with laz 1.7 and fpc 3.1.1; but only happens recently after an update. I have not thoroughly looked into this as its not a big issue. I have only checked the code sign; so I might try removing and re-assigning it.  Also may try disabling Anti Virus incase some issue. So a few things to try first. Also an odd behavior of laz 1.7 IDE; I have an application that when ever I add a component event; and compile it informs me of duplicate function; the ide has auto added in an empty function for one that all ready exists; So all I do is remove the auto created empty funtion and everything is fine; until I add another event. Not had this before; it may be a noted issue; that I cannot find probably by using the wrng search criteria. Also noted an issue; in windows If i use a font; ie Eurostile and i have it named eurostile ( note no Uppercase) it works fine in windows; on OSX it will not display the correct font. OSX requires the Font to be Exactly Correct (Upper/Lower case must be correct); windows ignores case.  Not a major problem; as I just edited the LFM in text editor and Corrected all font names so that they are Correct.