Native lib with singleton and C++/CLI program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joes.staal@gmail.com

    Native lib with singleton and C++/CLI program

    Hi,

    I know this has been asked earlier on, however, none of the other
    threads where I looked solved the following problem.
    1. I've got a native C++ library (lib, not a dll) with a singleton.
    2. I've got a C++/CLI program with a wrapper around some functions in
    the singleton of the native lib.
    3. When I run my program, the wrappers instantiate their own copy of
    the singleton, i.e.

    CLIAssembly::CL IObject::Value( 42); // Is a wrapper around
    Native::SingleO bject::instance ().value(int i)
    Native::SingleO bject::instance ().value(13);

    Console::WriteL ine("Value in CLI object is {0}",
    CLIAssembly::CL IObject::Value( ));
    Console::WriteL ine("Value in Native object is {0}",
    Native::SingleO bject::instance ().value());

    gives as output

    Value in CLI object is 42
    Value in Native object is 13

    Only when I put the source code of the native lib in the CLI/C++
    program project I get the behaviour I'd expect.

    I've tried various options:
    - Compiling the native lib with /clr
    - adding #pragma managed and #pragma unmanaged around the include for
    the native code in the CLI project.
    All to no avail.

    Any help and ideas would be much appreciated.

    Joes
  • Carl Daniel [VC++ MVP]

    #2
    Re: Native lib with singleton and C++/CLI program

    <joes.staal@gma il.comwrote in message
    news:10f0ad4d-da78-4086-9e19-ea1255be72f9@34 g2000hsf.google groups.com...
    Only when I put the source code of the native lib in the CLI/C++
    program project I get the behaviour I'd expect.
    >
    I've tried various options:
    - Compiling the native lib with /clr
    - adding #pragma managed and #pragma unmanaged around the include for
    the native code in the CLI project.
    All to no avail.
    >
    Any help and ideas would be much appreciated.
    How is your code linked? (i.e. is it a single monolightic EXE, EXE+DLL,
    multiple DLLs, etc) How is the singleton actually declared? Code
    defined in a library will, of course, be duplicated in each image (DLL, EXE,
    etc) that uses the library.

    If you've got two different values, you have one of two problems:
    1. Your wrapper isn't really wrapping what you think it is.
    2. Youe singleton is actually a doubleton.

    A short complete example (i.e. something that other can compile/link/run)
    would be very helpful in understanding where exactly the problem lies.

    -cd


    Comment

    • Joes

      #3
      Re: Native lib with singleton and C++/CLI program

      On Jul 15, 9:19 pm, "Carl Daniel [VC++ MVP]"
      <cpdaniel_remov e_this_and_nos. ..@mvps.org.nos pamwrote:
      <joes.st...@gma il.comwrote in message
      >
      news:10f0ad4d-da78-4086-9e19-ea1255be72f9@34 g2000hsf.google groups.com...
      >
      Only when I put the source code of the native lib in the CLI/C++
      program project I get the behaviour I'd expect.
      >
      I've tried various options:
      - Compiling the native lib with /clr
      - adding #pragma managed and #pragma unmanaged around the include for
      the native code in the CLI project.
      All to no avail.
      >
      Any help and ideas would be much appreciated.
      >
      How is your code linked?  (i.e. is it a single monolightic EXE, EXE+DLL,
      multiple DLLs, etc)   How is the singleton actually declared?    Code
      defined in a library will, of course, be duplicated in each image (DLL, EXE,
      etc) that uses the library.
      >
      If you've got two different values, you have one of two problems:
      1. Your wrapper isn't really wrapping what you think it is.
      2. Youe singleton is actually a doubleton.
      >
      A short complete example (i.e. something that other can compile/link/run)
      would be very helpful in understanding where exactly the problem lies.
      >
      -cd
      Hi Carl,

      I can post some code or a zip file somewhere with the projects
      tomorrow. The code setup is as follows:
      - lib with singleton (native C++ code)
      - exe with a public ref wrapper class that is abstract and sealed and
      two static methods that
      make a call to the singleton instance from the lib and set or get a
      value.

      Calling the singleton via the wrapper class gives different values
      than calling it directly,
      as I showed in the code snippet in my initial post.

      Anyway, I'll give some code tomorrow when I am back at my work
      machine.

      Thx,

      Joes

      Comment

      • Joes

        #4
        Re: Native lib with singleton and C++/CLI program

        A follow up. The issue I was encountering was the following. I have
        three projects, one producing a native static lib (LIB), one a .NET
        assembly (DLL) and one executable (EXE).
        The LIB implements a singleton, the DLL writes a wrapper around it.
        The EXE calls the singleton directly by linking in the LIB, and
        indirectly by referencing the wrapper DLL. It turns out the wrapper
        has its own copy of the singleton and so the EXE has two independent
        singletons.

        Turning the LIB into a native DLL and exporting the singleton removes
        the doubleton, giving the EXE access to the same instance by calling
        the wrapper as well as the direct call.

        I think I quite understand that, and I also know what to do to solve
        the issues I've got within my projects.

        Thx,

        Joes

        Comment

        • Carl Daniel [VC++ MVP]

          #5
          Re: Native lib with singleton and C++/CLI program

          Joes wrote:
          A follow up. The issue I was encountering was the following. I have
          three projects, one producing a native static lib (LIB), one a .NET
          assembly (DLL) and one executable (EXE).
          The LIB implements a singleton, the DLL writes a wrapper around it.
          The EXE calls the singleton directly by linking in the LIB, and
          indirectly by referencing the wrapper DLL. It turns out the wrapper
          has its own copy of the singleton and so the EXE has two independent
          singletons.
          >
          Turning the LIB into a native DLL and exporting the singleton removes
          the doubleton, giving the EXE access to the same instance by calling
          the wrapper as well as the direct call.
          >
          I think I quite understand that, and I also know what to do to solve
          the issues I've got within my projects.
          Yep - that's what I thought, based on your description. Glad you got it
          sorted - you're right that the only way to have a true in-process singleton
          is to implement it in a DLL.

          -cd


          Comment

          Working...