dmsurti / AssimpKit

A library (macOS, iOS) that converts the files supported by Assimp to Scene Kit scenes.
http://assimpkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
477 stars 54 forks source link

[Question] Is it possible to catch Assimp exceptions? #85

Closed anton-plebanovich closed 7 years ago

anton-plebanovich commented 7 years ago

Hi @dmsurti . I'm trying to catch Assimp C++ exceptions using @try {} @catch (...) {} block but for some reason exceptions do not reach it and application just crashes instead.

Sample model: https://s3-eu-west-1.amazonaws.com/show-it/candle.dae

Sample code in GameViewController.m using AssimpKit_Process_ValidateDataStructure step:

    SCNAssimpScene *scene = nil;
    // Load the scene
    @try {
        scene =
        [SCNScene assimpSceneWithURL:[NSURL URLWithString:self.modelFilePath]
                    postProcessFlags:
        AssimpKit_Process_FlipUVs |
        AssimpKit_Process_Triangulate |
        AssimpKit_Process_ValidateDataStructure
         ];
    } @catch (...) {
        NSLog(@"CATCH");
    }

Stack Trace: image

* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x0000000112882d42 libsystem_kernel.dylib`__pthread_kill + 10
    frame #1: 0x00000001128ba457 libsystem_pthread.dylib`pthread_kill + 90
    frame #2: 0x00000001125420e7 libsystem_c.dylib`abort + 127
    frame #3: 0x000000011250a778 libsystem_c.dylib`__assert_rtn + 320
    frame #4: 0x000000010c4e9794 AssimpKit`Assimp::ValidateDSProcess::ReportError(char const*, ...) + 548
    frame #5: 0x000000010c4eb0ec AssimpKit`Assimp::ValidateDSProcess::Validate(aiMesh const*) + 892
    frame #6: 0x000000010c4ea5f4 AssimpKit`void Assimp::ValidateDSProcess::DoValidation<aiMesh>(aiMesh**, unsigned int, char const*, char const*) + 180
    frame #7: 0x000000010c4e9cc9 AssimpKit`Assimp::ValidateDSProcess::Execute(aiScene*) + 121
    frame #8: 0x000000010c3ff682 AssimpKit`Assimp::BaseProcess::ExecuteOnScene(Assimp::Importer*) + 274
    frame #9: 0x000000010c4175e4 AssimpKit`Assimp::Importer::ReadFile(char const*, unsigned int) + 7028
    frame #10: 0x000000010c3ca439 AssimpKit`aiImportFileExWithProperties + 745
    frame #11: 0x000000010c3ca147 AssimpKit`aiImportFileEx + 39
    frame #12: 0x000000010c3ca10f AssimpKit`aiImportFile + 31
  * frame #13: 0x000000010c3c131b AssimpKit`-[AssimpImporter importScene:postProcessFlags:](self=0x0000600000478780, _cmd="importScene:postProcessFlags:", filePath=@"/Users/mac-246/Library/Developer/CoreSimulator/Devices/9362EE01-7550-4A4B-BE56-2F7003A37995/data/Containers/Data/Application/A981F6DE-1092-4BC2-AF24-2E9D54AD53FA/Documents/candle.dae", postProcessFlags=8389640) at AssimpImporter.m:131
    frame #14: 0x000000010c3c9e63 AssimpKit`+[SCNScene(self=SCNScene, _cmd="assimpSceneWithURL:postProcessFlags:", url=@"/Users/mac-246/Library/Developer/CoreSimulator/Devices/9362EE01-7550-4A4B-BE56-2F7003A37995/data/Containers/Data/Application/A981F6DE-1092-4BC2-AF24-2E9D54AD53FA/Documents/candle.dae", postProcessFlags=8389640) assimpSceneWithURL:postProcessFlags:] at SCNScene+AssimpImport.m:120
    frame #15: 0x000000010c0cfdad iOS-Example`-[GameViewController viewDidLoad](self=0x00007fec7050c720, _cmd="viewDidLoad") at GameViewController.m:51
    frame #16: 0x000000010f2838a5 UIKit`-[UIViewController loadViewIfRequired] + 1235
    frame #17: 0x000000010f283cf2 UIKit`-[UIViewController view] + 27
    frame #18: 0x000000010f2b5d4e UIKit`-[UINavigationController _startCustomTransition:] + 954
    frame #19: 0x000000010f2cc5a4 UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 686
    frame #20: 0x000000010f2cd8c3 UIKit`-[UINavigationController __viewWillLayoutSubviews] + 150
    frame #21: 0x000000010f526a49 UIKit`-[UILayoutContainerView layoutSubviews] + 231
    frame #22: 0x000000010f1af6f5 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1439
    frame #23: 0x000000011213a3ee QuartzCore`-[CALayer layoutSublayers] + 153
    frame #24: 0x000000011213e4dd QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 401
    frame #25: 0x00000001120c6ded QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 365
    frame #26: 0x00000001120f2704 QuartzCore`CA::Transaction::commit() + 500
    frame #27: 0x000000010f108ca3 UIKit`_afterCACommitHandler + 272
    frame #28: 0x000000010e5e5d37 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    frame #29: 0x000000010e5e5c8e CoreFoundation`__CFRunLoopDoObservers + 430
    frame #30: 0x000000010e5ca254 CoreFoundation`__CFRunLoopRun + 1572
    frame #31: 0x000000010e5c99b9 CoreFoundation`CFRunLoopRunSpecific + 409
    frame #32: 0x00000001154729c6 GraphicsServices`GSEventRunModal + 62
    frame #33: 0x000000010f0de5e8 UIKit`UIApplicationMain + 159
    frame #34: 0x000000010c0d11cf iOS-Example`main(argc=1, argv=0x00007fff53b30190) at main.m:43
    frame #35: 0x000000011246fd81 libdyld.dylib`start + 1

Is there any way to prevent application crash on exception?

dmsurti commented 7 years ago

@anton-plebanovich I am able to load this .dae file fine, albeit without the textures.

screenshot 2017-11-08 23 00 02

I think right now validateDataStructure flag is causing the import to fail, as it is a strict validation.

However, yes, the API can definitely be improved bringing it in line with how native SCNScene API works by passing in an NSError object, which will contain errors reported by assimp.

Thanks for reporting this with a valid model file.

anton-plebanovich commented 7 years ago

Thanks for the answer @dmsurti !

anton-plebanovich commented 7 years ago

App doesn't crash with latest libassimp-fat.a lib I built from source.

@dmsurti Are you using something different from master branch? My model after conversion looks like this: simulator screen shot - iphone se - 2017-11-09 at 13 14 40

anton-plebanovich commented 7 years ago

Created pull request with swift-compatible error processing - https://github.com/dmsurti/AssimpKit/pull/86

dmsurti commented 7 years ago

Are you using something different

@anton-plebanovich Yes, that is from a different branch I was working on for the next milestone.

dmsurti commented 7 years ago

pull request with swift-compatible error processing

Awesome. Thanks for the effort 👍 . I will be able to review this on Mon, will let you know my comments and/or merge this.

dmsurti commented 7 years ago

Fixed in #86.