Sharing DLL between threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ram P. Dash

    Sharing DLL between threads

    Hi:

    I've a third party DLL (not a .NET class library PE) which I use using
    DllImport attribute in my application running in multiple threads invoking
    different methods of the same DLL.

    The memory usage of the application is shooting to 1GB in just 30 minutes.
    It goes till it's 1.4GB after which I get OutOfMEmory exceptions. If I don't
    use the DLL, my memory stays put at 100MB.

    I tried freeing the DLL using FreeLibrary but after few seconds I am getting
    "Object reference not set to an instance of an object" while accessing the
    methods of the DLL.

    How can I ensure that only one copy of the DLL per thread is in my
    application memory and that the DLL is unloaded when it's no longer used by
    any of the threads?

    BTW, my application is an ASP.NET web service but this has nothing to do
    with the statelessness of a webmethod because I am spawning and running the
    threads in the background at application level.

    Thanks,
    Ram


  • Lloyd Dupont

    #2
    Re: Sharing DLL between threads

    if you use standart PInvokde mechanism: DON'T EVER FREE your LIBRARY
    amd it can't be unloaded.
    but don't wory, there is, already, one single instance of the DLL in memory
    for all thread (of all process!!!)

    if you memory goes that up, it's very likely to be a bug in your application

    DLL used with standart mechanism won't upload and DON'T EVER call a function
    in a FREED Library.
    That will crash your program instantly or, worst, cause weird, mysterious,
    alien bugs...


    On the other hand, with .NET 2.0 you could transform IntPtr (of a function
    pointer) to a delegate.
    So you could used LoadLibrary/FreeLibrary & GetProcAddress.
    But even then, NEVER call into a FREED library.


    "Ram P. Dash" <rampr2@hotmail .com> wrote in message
    news:oCvle.1050 $MI4.716@newsre ad2.news.pas.ea rthlink.net...[color=blue]
    > Hi:
    >
    > I've a third party DLL (not a .NET class library PE) which I use using
    > DllImport attribute in my application running in multiple threads invoking
    > different methods of the same DLL.
    >
    > The memory usage of the application is shooting to 1GB in just 30 minutes.
    > It goes till it's 1.4GB after which I get OutOfMEmory exceptions. If I
    > don't use the DLL, my memory stays put at 100MB.
    >
    > I tried freeing the DLL using FreeLibrary but after few seconds I am
    > getting "Object reference not set to an instance of an object" while
    > accessing the methods of the DLL.
    >
    > How can I ensure that only one copy of the DLL per thread is in my
    > application memory and that the DLL is unloaded when it's no longer used
    > by any of the threads?
    >
    > BTW, my application is an ASP.NET web service but this has nothing to do
    > with the statelessness of a webmethod because I am spawning and running
    > the threads in the background at application level.
    >
    > Thanks,
    > Ram
    >[/color]


    Comment

    Working...