C4D hangs during exit with our plugin on MAC



  • On 04/06/2014 at 23:35, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   R15 
    Platform:   Windows  ;   Mac OSX  ; 
    Language(s) :     C++  ;

    ---------
    As part of our FUSEE 3D OpenSource game engine project we developed a CINEMA 4D plugin exporting the currently visible geometry and part of the material settings to an interactive HTML 5/WebGL application (see http://fusee3d.org/c4dexporter/). On Windows everything works pretty well (download the free plugin and try it if you like to).

    While porting the plugin to MAC OS, I encountered some strange behavior. The plugin works well on MAC, only when closing CINEMA 4D, the application hangs and needs to be killed manually. Debugging shows that our plugin receives the C4DPL_END message and properly passes PluginEnd, FreeHelpDelegates, FreeResources and DeleteObj. The crash occurs afterwards somewhere inside CINEMA4D code. I attached the disassembly below. The following exception occurs at the mov instruction in the red/bold/underlined code line: "Thread 1: EXC_BAD_ACCESS (code=1, address=0x113d6a210).

    Since I am pretty new to MAC development in general and especially C4D plugin development on MAC I would be happy for any suggestion how to further track down that problem.

    Christoph

    tl;tr:
    Some background information: The functionality of our PlugIn is developed in .NET/C#. To be able to run it within C4D we wrapped huge parts of the C4D-C++-API to C# (currently 700 types (classes/enums/structs) with more than 1000 methods). We do this automatically using the SWIG wrapper generator. To actually run the plugIn  we developed a generic C4D-C++ plugin that starts up the .NET runtime OR the Mono runtime on Windows (user may choose) which both works pretty well. On MAC only the mono runtime is available, since .NET is Microsoft-Land. As already said, everything works fine on MAC as well - even a hand-coded C# local web-server running inside the plugin worked on MAC right from the start as compiled on Windows(!), only with the resulting crash on shutdown. I completely stripped out our C#-functionality and observed that the crash happens if only the mono runtime is started during our C++part of the plugin without performing any of our C# custom code. So the question is: what could the mono runtime do that makes C4D crash once the mono runtime is shutdown without any problems? Are there any debug symbols available to identify all the __lldb_unamed_function$$CINEMA 4D listed in the disassembly below? Thanks!

    CINEMA 4D`___lldb_unnamed_function1$$CINEMA 4D:
    0x100001970:  pushq  $0x0
    0x100001972:  movq   %rsp, %rbp
    0x100001975:  andq   $-0x10, %rsp
    0x100001979:  movq   0x8(%rbp), %rdi
    0x10000197d:  leaq   0x10(%rbp), %rsi
    0x100001981:  movl   %edi, %edx
    0x100001983:  addl   $0x1, %edx
    0x100001986:  shll   $0x3, %edx
    0x100001989:  addq   %rsi, %rdx
    0x10000198c:  movq   %rdx, %rcx
    0x10000198f:  jmp    0x100001995               ; ___lldb_unnamed_function1$$CINEMA 4D + 37
    0x100001991:  addq   $0x8, %rcx
    0x100001995:  cmpq   $0x0, (%rcx)
    0x100001999:  jne    0x100001991               ; ___lldb_unnamed_function1$$CINEMA 4D + 33
    0x10000199b:  addq   $0x8, %rcx
    0x10000199f:  callq  0x100a5aad0               ; ___lldb_unnamed_function22807$$CINEMA 4D
    0x1000019a4:  movl   %eax, %edi
    0x1000019a6:  callq  0x100cb3352               ; symbol stub for: exit
    0x1000019ab:  hlt    
    0x1000019ac:  nop    
    0x1000019ad:  nop    
    0x1000019ae:  nop    
    0x1000019af:  nop



  • On 05/06/2014 at 09:46, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    <ADDRESS>
    User Information:
    Cinema 4D Version:   R15 
    Platform:   Windows  ;  
    Mac OSX  ; 
    Language(s) :    
    C++  ;

    ---------
    </ADDRESS> As part of our FUSEE 3D OpenSource game engine project we developed a CINEMA 4D plugin exporting the currently visible geometry and part of the material settings to an interactive HTML 5/WebGL application (see http://fusee3d.org/c4dexporter/). On Windows everything works pretty well (download the free plugin and try it if you like to). 
    While porting the plugin to MAC OS, I encountered some strange behavior. The plugin works well on MAC, only when closing CINEMA 4D, the application hangs and needs to be killed manually. Debugging shows that our plugin receives the C4DPL_END message and properly passes PluginEnd, FreeHelpDelegates, FreeResources and DeleteObj. The crash occurs afterwards somewhere inside CINEMA4D code. I attached the disassembly below. The following exception occurs at the mov instruction in the red/bold/underlined code line: "Thread 1: EXC_BAD_ACCESS (code=1, address=0x113d6a210).
    Since I am pretty new to MAC development in general and especially C4D plugin development on MAC I would be happy for any suggestion how to further track down that problem. [...]

    Please submit the crash report via the built-in crash reporter and refer to this thread in the description of the problem. We then can have a look at the source code.

    Possible problems:
    - Your code is using global variables
    - Your code is accessing memory that ain't available anymore
    - Your code has a thread running although the plugin code is already unloaded (and it runs in already freed memory)
    - ...

    Best regards,

    Wilfried



  • On 06/06/2014 at 06:04, xxxxxxxx wrote:

    Wilfried,
    thank you very much for your helpful advice. In order to generate a crash report I started trying to reproduce my problem which I have been observing for two weeks now, ...but: strictly following murphy's law today I get a totally different behavior: Either Cinema4D closes without any trouble OR it crashes during the shutdown of mono (and not AFTER it as described in my original post). So I will try to investigate further. Anyway, this unpredictable behavior is most likely caused by threading race conditions, so I will give the third "possible problem" you listed a try.

    Just to get a bit more understanding: Do global variables need to be avoided under all circumstances. Since I am starting up the mono runtime, a lot of things listed under your "possible problems" might take place: At least I am pretty sure that global/static variables will be used inside the mono runtime. Isn't it sufficent to make sure that all recources (threads, memory, etc.) will be released during PluginEnd and not touched any more after that?

    Thanks again,
    Christoph



  • On 06/06/2014 at 09:35, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    [...] Isn't it sufficent to make sure that all recources (threads, memory, etc.) will be released during PluginEnd and not touched any more after that?
    Thanks again,
    Christoph

    That 's sufficient. Global variable will create trouble when they consist of more than POD (plain old data type like int, char, etc.) as classes (e.g. just a string) will allocated/deallocate memory out of this plugin init/end scope.

    Best regards,

    Wilfried


Log in to reply