Search code examples
c#xamarin.iosbarcodebarcode-scanner

Using RedLaser SDK via MonoTouch crashing as soon as BarcodePickerController shown


I'm using Chris Branson's RedLaser MonoTouch bindings and sample from here: https://github.com/chrisbranson/RedLaserSample

Firstly, to help out those also having trouble with this sample project, I had to update both the makefile to build the RedLaser.dll MonoTouch bindings and the sample MonoDevelop csproj to get the actual app compiling.

  • Makefile
    The makefile supplied by Chris unzipped the RedLaser SDK download, but RedLaser changed their naming convention and so it was no longer working. I simplified it, instead requiring the user to put the libRedLaserSDK.a file into the build directory manually:

    BTOUCH=/Developer/MonoTouch/usr/bin/btouch
    BUILDVER=build139
    VERSION=3.2.4
    BINDIR=./bin/
    
    all: $(BINDIR)RedLaser.dll
    
    $(BINDIR)libRedLaserSDK.a: 
        @echo "You'll need to obtain a copy of RedLaserDevSDK-$(VERSION).zip from"
        @echo "http://redlaser.com/sdk/index.php"
        @echo Locate the libRedLaserSDK.a file and place within
        @echo $(BINDIR)
        @echo 
        @open http://redlaser.com/sdk/index.php
        @exit 1
    
    $(BINDIR)RedLaser.dll: Makefile AssemblyInfo.cs RedLaser.cs enums.cs $(BINDIR)libRedLaserSDK.a
        $(BTOUCH) --out=$@ -e RedLaser.cs enums.cs AssemblyInfo.cs --link-with=$(BINDIR)libRedLaserSDK.a,libRedLaserSDK.a
    
    clean:
        -rm -rf $(BINDIR)*.dll  
    

    The directory containing the makefile should also contain the files redlaser.cs, enums.cs and AssemblyInfo.cs (i.e. the same as Chris's).

  • MonoDevelop project build settings
    I think the reason this stopped working was a bug in MonoDevelop or MonoTouch and the way it expands the 'Additional mtouch arguments' when they contained quotation marks. Chris's original arguments looked like this:

    -cxx -gcc_flags="-framework SystemConfiguration -framework CFNetwork -framework CoreVideo -framework CoreMedia -framework AVFoundation -framework OpenGLES -framework Security -L${ProjectDir}/Lib -lRedLaserSDK -ObjC"  
    

    MonoDevelop doesn't parse this correctly, instead inserting quotation marks around each option separated by a space. Looking in the build output (under 'Compiling to native code') showed that this got expanded to:

    "/Users/tyson/Downloads/chrisbranson-RedLaserSample-1a5545f-1/RedLaserSample/Lib/RedLaser.dll" -debug -nolink -sdk "5.1" -targetver "5.1" --armv7 "-cxx" "-gcc_flags=\"-framework" "SystemConfiguration" "-framework" "CFNetwork" "-framework" "CoreVideo" "-framework" "CoreMedia" "-framework" "AVFoundation" "-framework" "OpenGLES" "-framework" "Security" "-L/Users/tyson/Downloads/chrisbranson-RedLaserSample-1a5545f-1/RedLaserSample/Lib" "-lRedLaserSDK" "-ObjC\""  
    

    The solution involves adding explicit quotation marks around the whole 'gcc_flags' option, manually escaping the inner quotation marks. e.g:

    "-cxx" "-gcc_flags=\"-framework SystemConfiguration -framework CFNetwork -framework CoreVideo -framework CoreMedia -framework AVFoundation -framework OpenGLES -framework Security -L${ProjectDir}/Lib -lRedLaserSDK -ObjC\""  
    

    This expands correctly into:

    "/Users/tyson/Downloads/chrisbranson-RedLaserSample-1a5545f-1/RedLaserSample/Lib/RedLaser.dll" -debug -nolink -sdk "5.1" -targetver "5.1" --armv7 "-cxx" "-gcc_flags=\"-framework SystemConfiguration -framework CFNetwork -framework CoreVideo -framework CoreMedia -framework AVFoundation -framework OpenGLES -framework Security -L/Users/tyson/Downloads/chrisbranson-RedLaserSample-1a5545f-1/RedLaserSample/Lib -lRedLaserSDK -ObjC\""
    

Ok, so now that I had it compiling I attempted to run it on my phone. The app loads up fine, and successfully calls into the RedLaser SDK via the RedLaserSDK.CheckReadyStatus() function. But as soon as I hit the scan button (which loads the BarcodePickerController) it crashes with a native exception deep within the RedLaser SDK:

terminate called throwing an exception
Native stacktrace:

0   RedLaserSample                      0x0159d62d mono_handle_native_sigsegv + 244
1   RedLaserSample                      0x015b6251 sigabrt_signal_handler + 112
2   libsystem_c.dylib                   0x319b67ed _sigtramp + 48
3   libsystem_c.dylib                   0x319ac20f pthread_kill + 54
4   libsystem_c.dylib                   0x319a529f abort + 94
5   libc++abi.dylib                     0x34fb1f6b abort_message + 46
6   libc++abi.dylib                     0x34faf34d _ZL17default_terminatev + 24
7   libobjc.A.dylib                     0x358e536f _objc_terminate + 170
8   libc++abi.dylib                     0x34faf3c5 _ZL19safe_handler_callerPFvvE + 76
9   libc++abi.dylib                     0x34faf451 _ZdlPv + 0
10  libc++abi.dylib                     0x34fb079f __cxa_throw + 122
11  RedLaserSample                      0x000404a9 _ZN5zxing6qrcode19FinderPatternFinder18selectBestPatternsEv + 172
12  RedLaserSample                      0x000415e9 _ZN5zxing6qrcode19FinderPatternFinder4findERKNS_11DecodeHintsE + 512
13  RedLaserSample                      0x0003a7f7 _ZN5zxing6qrcode8Detector6detectERKNS_11DecodeHintsE + 366
14  RedLaserSample                      0x00046b97 _ZN5zxing6qrcode12QRCodeReader6decodeENS_3RefINS_12BinaryBitmapEEENS_11DecodeHintsE + 130
15  RedLaserSample                      0x0004ec35 _ZN5zxing6Reader6decodeENS_3RefINS_12BinaryBitmapEEE + 240
16  RedLaserSample                      0x00058455 -[FormatReader decode:] + 116
17  RedLaserSample                      0x00013835 -[ZXingDecoder findCodesInBitmap:bytesPerRow:width:height:] + 656
18  RedLaserSample                      0x0005f6af -[BarcodePhotoEngine zxingFindBarcodesInPixmap:] + 222
19  RedLaserSample                      0x00055f25 __38-[BarcodeEngine findBarcodesInPixMap:]_block_invoke_059 + 76
20  libdispatch.dylib                   0x34635c59 _dispatch_call_block_and_release + 12
21  libdispatch.dylib                   0x34637d0f _dispatch_queue_drain + 274
22  libdispatch.dylib                   0x34637b75 _dispatch_queue_invoke$VARIANT$mp + 40
23  libdispatch.dylib                   0x346387e7 _dispatch_worker_thread2 + 210
24  libsystem_c.dylib                   0x31967dfb _pthread_wqthread + 294
25  libsystem_c.dylib                   0x31967cd0 start_wqthread + 8

=================================================================  
Got a SIGABRT while executing native code. This usually indicates  
a fatal error in the mono runtime or one of the native libraries  
used by your application.  
=================================================================

Running the very similar RedLaser Xcode sample project (i.e. no MonoTouch involvement, all objective-c) works fine.

Also, if it is of any significance, disabling all barcode scan types allows the BarcodePickerController to load up and display the camera view. Obviously it never detects a barcode though.


Solution

  • The crash you mention has been fixed in the bindings now.

    It was related to QR's usage of C++ exceptions and incorrect linking arguments.