Closed: Frustrating Peculiar Problem: Use of Static Member Variables of an Unmanaged Class Inside an Assembly RRS feed

  • Question

  • I have simplified my problem to a very repeatable test.
    It boils down to the following:
    1. Cefine and implement a native c++ class that has a static member variable. Make this project a static lib.
    2. Create a C++/CLR client (let's call this ManagedClient1)for the above class. Say implement this in the form of an exe. Initialize the native c++ class.
    (let's say your native static member is now set to (int)1)
    3. Run a test. The test works perfectly. The native class gets initialized while the static member's value is preserved for subsequent calls.
    4. Create a second c++/CLR client for the native class. We'll call this ManagedClient2. Make this project a DLL. Write a class to use services or your native c++ class.
    5. Change ManagedClient1 to include a reference to ManagedClient2.
    6. Change ManagedClient1 to use ManagedClient2's classes to access your native class.
    7. What you'll observe is that the value of the static member is always reset between calls to your native class!

    If you have an idea of what is going on here please let me in on it too.

    I can paste all the relevant files if you don't think this makes sense.
    • Edited by j_saremi Thursday, December 25, 2008 12:25 AM
    Tuesday, December 23, 2008 7:24 PM


  • The extra indirection makes my brain hurt.  Each DLL would have its own copy of the static member, they are not shared.
    Hans Passant.
    Tuesday, December 23, 2008 10:52 PM

All replies