dcsimg
Results 1 to 3 of 3

Thread: Does this matter?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2019
    Posts
    64

    Does this matter?

    In this scenario I create some classes in a worker thread that contain data that I use to determine if I still need to keep them around. I store these classes in a dictionary.

    In the main thread i periodically clean up the ones that I no longer need. I see the class creation event fire in the worker thread and the termination event fire in the main thread.

    I am chasing a memory leak that might be related to this. In single threaded mode where these classes are all created and destroyed in the main thread private bytes is stable.

    In multi threaded mode where the class is created by the worker and destroyed by the main it leaks.

    All the code is the same in both cases.

    I am not sure if I am breaking some com rule which is causing stuff to hang around even though terminate fires and cleans up data etc.

    I would appreciate some opinions.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,405

    Re: Does this matter?

    Are you actually multithreading? If so, how? There are a number of approaches ranging from an ActiveX EXE in STA or MTA mode, creating separate Apartments via some library like newObjectsDevelopment Library (NDL), a bunch of raw API calls, etc.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2019
    Posts
    64

    Re: Does this matter?

    Thanks for your reply. And I have the answer. Yes it is actually multi-threaded code using mthreadvb.

    https://www.planet-source-code.com/v...26900&lngWId=1

    As I said the class is stored in a scripting dictionary and the class destruction is triggered by removing the key from the dictionary. I hacked my code last night so that instead of removing the key in the main thread I do the following:

    1) When the object is created the same key used to access the object is placed into another dictionary with the value being the owning thread
    2) When its time to get rid of the class the main thread instead of removing the key from the dictionary holding the class it adds the same key to another dictionary
    3) The worker then checks step 2 dictionary for entries, checks if it is the owner using the dictionary entry in 1 above and removes the original class key, the ownership key and the delete key added in step 2.

    12 hours later no leaks at all. Stable private bytes.

    What I don't understand, and I would really like to, is that although the termination event for the class fires regardless of which thread the key is remove from dictionary holding the class (main or worker) it only really cleans up properly if the key is removed by the thread that created the class.

    This may be a function of the way mthreadvb works or it might just be COM rules. Either way I would like to understand why if someone knows and I hope this helps someone else not waste days looking for leaks of this nature.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width