issues with DLL and STL containers



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/05/2010 at 01:46, xxxxxxxx wrote:

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

    ---------
    Hey everyone,

    my name is Heinrich Löwe, im working for Sehsucht GmbH in Hamburg. Me and a workmate(Martin Chatterjee) are working on a multi-platform-exchange format. For this reason we have written a DLL library which containes the basic functionality. It is imported in plugins for various applications and c4d is one of them.

    I don't want to bore anyone with more details and will get straight to the point: The DLL is exporting functions which modify references to STL containers. If i use these functions in a C4D plugin, it always leads to crashes. However, they work fine with a direct inlcude of the code or in different environments than c4d.

    I've boiled down the code to show you the main point:

    Parts of the DLL library which are responsible for the problems:

      
    // ...   
      
    // define a dll import/export macro   
    #ifdef DLL_TEST_EXPORTS   
       #define DLL_MACRO    __declspec(dllexport)   
    #else   
       #define DLL_MACRO    __declspec(dllimport)   
    #endif   
      
    // ...   
      
    // A representative structure exporting a stl container   
    struct DLL_MACRO MyStruct {   
      
         template class DLL_MACRO allocator<int>;   
         template class DLL_MACRO vector<int, allocator<int> >;   
      
         vector<int> vec;   
    };   
      
    // ...   
      
    // Global function which modifies the given reference   
    void DLL_MACRO GlobalFunction(vector<MyStruct> &vec;)   
    {   
         // just do something with the input   
         vec.resize(10);   
    }   
      
    // ...   
    

    Now the usage of the exported function in the C4D plugin code:

      
    // ...   
      
    // code is executed during the c4d startup phase, while   
    // registering the plugin.   
    Bool Register_MyTestPlugin(void)   
    {   
         // create some stl container, using the exported structure   
         vector<MyStruct> *b = gNew vector<MyStruct>;   
      
         // call exported function which modifies my stl container   
         GlobalFunction(*b);   
            
         // kill it, at this point the plugin crashes   
         /////////////////////////////////////////////////////////   
         gDelete(b);   
         /////////////////////////////////////////////////////////   
      
         // ...   
      
         return TRUE;   
    }   
      
    // ...   
    

    While running that plugin in debug mode everything works, up to the point where the vector is deleted. I tracked it further down to a call to C4DOS.Ge->Free() in the overloaded delete operator in the file 'c4d_memory.cpp'.

    Thas is the point where i got stuck and i'm hoping for your assistance to solve the problem,

    Thanks,
    Heinrich Löwe.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 26/05/2010 at 06:19, xxxxxxxx wrote:

    Which Cinema 4D version are you using? My test DLL with Cinema 4D R11.5 seems to work fine.

    cheers,
    Matthias



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 27/05/2010 at 01:16, xxxxxxxx wrote:

    I'm using R11 64bit on a windows 7 machine.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 27/05/2010 at 03:04, xxxxxxxx wrote:

    Ok, I can confirm the crashing on Cinema R11. Afaik it is due to the overloading of new/delete by the Cinema 4D SDK. Since R11.5 this is not the case anymore, making it easier to link to DLLs/DYLIBs. I am not sure if there is a solution for R11. I will forward the issue to the developers maybe they know a solution of the problem.

    cheers,
    Matthias



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 27/05/2010 at 05:14, xxxxxxxx wrote:

    Strange now it crashes under R11.5 too. I'll have to ask the developers what is going on.

    cheers,
    Matthias



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/05/2010 at 09:53, xxxxxxxx wrote:

    I upgraded to R11.5, and the test plugin (together with the test DLL) works fine now.  But the real plugin still crashes with the following message:

    Unbehandelte Ausnahme bei 0x000007fefd6caa7d in CINEMA 4D 64 Bit.exe: Microsoft C++-Ausnahme: std::bad_alloc an Speicherposition 0x02c25740..



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/05/2010 at 13:54, xxxxxxxx wrote:

    try & catch?  ;) What about a little bit of source code that triggered the crash?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 29/05/2010 at 06:31, xxxxxxxx wrote:

    Hello again,

    i debugged the code this morning, made some changes and it seems to work fine. However, there is still one thing which leads to crashes: Whenever i pass a std::string to the dll, the plugin stops executing, but it does work fine when it's passed by reference.

    This does not work:

      
    // method declaration in the dll (parameter passed directly)  
    bool openFile(string sFullPath);  
      
    // ...  
      
    // call it from the plugin  
    std::string str("some filename in here");  
    if (reader.openFile(str)) {  
      // ...  
    }  
      
    /* the plugin crashes when the method openFile() returns to the caller an the  
     * local copy of the passed std::string is deleted.  
     */  
    

    This works:

      
    // method declaration in the dll (parameter passed by reference)  
    bool openFile(string &sFullPath);  
      
    // ...  
      
    // call it from the plugin  
    std::string str("some filename in here");  
    if (reader.openFile(str)) {  
        // ...  
    }  
    

    I 'solved' the problem by changing everything to 'call by reference' in the dll, but it doesn't feel good having some unexplainable error somewhere around.

    Greetings,
    Heinrich Löwe.


Log in to reply