C++ singletons and .NET AppDomains

Discussion in 'ASP .Net' started by Yevgeny Menaker, Jan 10, 2004.

  1. I am struggling with the following:
    We have legacy classes written in C++ that are compiled to static
    libraries. Some of these classes are singletons. In order to make
    these classes available to C#, we wrapped them with Managed C++. The
    wrapper classes works fine in most of the cases. However, if they are
    used in ASP.NET Web Applications a problem may arise in the following
    scenario:
    Step 1: The first Web Application starts in its own AppDomain inside
    aspnet_wp.exe process and creates wrapper singleton class, which in
    turn creates underlying unmanaged C++ singleton.
    Step 2: We change the web.config file of our Web Application. This
    causes unloading AppDomain of the Web Application.
    Step 3: Web Application starts again and a new AppDomain is loaded
    upon HTTP request.
    Step 4: A new AppDomain gets an instance of Singleton. Since the
    underlying C++ singleton has a static variable holding pointer to
    singleton instance, this instance lives until aspnet_wp.exe process
    exits. So, when trying to access this instance from a new AppDomain
    raises AppDomainUnloadedException, since we access memory that was
    allocated for an old AppDomain.


    The solution I found is using Remoting and create wrapper Singleton
    together with the underlying C++ singleton in the "remote" AppDomain.
    However, there is too much overhead. Another way is, of course, to
    rewrite the unmanaged C++ class and make it non-singleton.

    Are there any other cleaner solutions for this problem?

    Many thanks,
    Yevgeny Menaker
    Author, Software Engineer.
     
    Yevgeny Menaker, Jan 10, 2004
    #1
    1. Advertising

  2. Yevgeny Menaker

    Alvin Bruney Guest

    why don't you update your logic to cause the singleton instance to go away
    when the application domain unloads?

    --
    Regards,
    Alvin Bruney
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Yevgeny Menaker" <> wrote in message
    news:...
    > I am struggling with the following:
    > We have legacy classes written in C++ that are compiled to static
    > libraries. Some of these classes are singletons. In order to make
    > these classes available to C#, we wrapped them with Managed C++. The
    > wrapper classes works fine in most of the cases. However, if they are
    > used in ASP.NET Web Applications a problem may arise in the following
    > scenario:
    > Step 1: The first Web Application starts in its own AppDomain inside
    > aspnet_wp.exe process and creates wrapper singleton class, which in
    > turn creates underlying unmanaged C++ singleton.
    > Step 2: We change the web.config file of our Web Application. This
    > causes unloading AppDomain of the Web Application.
    > Step 3: Web Application starts again and a new AppDomain is loaded
    > upon HTTP request.
    > Step 4: A new AppDomain gets an instance of Singleton. Since the
    > underlying C++ singleton has a static variable holding pointer to
    > singleton instance, this instance lives until aspnet_wp.exe process
    > exits. So, when trying to access this instance from a new AppDomain
    > raises AppDomainUnloadedException, since we access memory that was
    > allocated for an old AppDomain.
    >
    >
    > The solution I found is using Remoting and create wrapper Singleton
    > together with the underlying C++ singleton in the "remote" AppDomain.
    > However, there is too much overhead. Another way is, of course, to
    > rewrite the unmanaged C++ class and make it non-singleton.
    >
    > Are there any other cleaner solutions for this problem?
    >
    > Many thanks,
    > Yevgeny Menaker
    > Author, Software Engineer.
     
    Alvin Bruney, Jan 17, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Microsoft

    aspnet_wp.exe and AppDomains

    Microsoft, Aug 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Microsoft
    Aug 27, 2003
  2. Yevgeny Menaker

    C++ singletons and .NET AppDomains

    Yevgeny Menaker, Jan 10, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    331
    Yevgeny Menaker
    Jan 10, 2004
  3. Yevgeny Menaker

    C++ singletons and .NET AppDomains

    Yevgeny Menaker, Jan 10, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    297
    Yevgeny Menaker
    Jan 10, 2004
  4. Joe Fallon

    Eval code and AppDomains

    Joe Fallon, Jan 25, 2005, in forum: ASP .Net
    Replies:
    18
    Views:
    1,101
    Joe Fallon
    Jan 31, 2005
  5. mortb
    Replies:
    1
    Views:
    315
    Michael Nemtsev
    May 3, 2007
Loading...

Share This Page