Skip to main content
deleted 11 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I try to work around problems like this by restructuring the program. I'll give examples that work with multiple threads since those examples will also work single threaded.

Example 1: One thread (main thread) creates and maintains a linked list of items. Each item may or may not have a separate thread (item thread) that works on that object. When the item thread is done, it doesn't delete its associated item object. Instead, it sets a DeleteMe=true on that item. The main thread occasionally looks for items to delete on its linked list.

Example 2: One thread is adding items to a circular linked list. Another thread is processing items on that linked list. When an item gets processed, it's not deleted from the linked list. Instead, it's marked with UnsedItem=true and moves on to the next item. The thread that adds items to the circular linked list adds new nodes if the list isn't long enough. The list starts out circular with 3 unused nodes.

  1. One thread (main thread) creates and maintains a linked list of items. Each item may or may not have a separate thread (item thread) that works on that object. When the item thread is done, it doesn't delete its associated item object. Instead, it sets a DeleteMe=true on that item. The main thread occasionally looks for items to delete on its linked list.

  2. One thread is adding items to a circular linked list. Another thread is processing items on that linked list. When an item gets processed, it's not deleted from the linked list. Instead, it's marked with UnsedItem=true and moves on to the next item. The thread that adds items to the circular linked list adds new nodes if the list isn't long enough. The list starts out circular with 3 unused nodes.

But I guess the simplest way to answer your question would be to use reference counting. You can have a Deleted bit on the object when you want to delete it. Other users of the object will remove their reference to it when they notice the Deleted bit. The object will be deleted when the ReferenceCount == 0ReferenceCount == 0. Only then, do you unload the dll.

You could create a linked list of pointers that are pointing to your object. Every time a new pointer points to the object, that pointer's address is added to your linked list. When the main object gets deleted, walk through the linked list setting all the pointers to that object to NULLNULL. This is getting into some kind of primativeprimitive garbage collection though.

I try to work around problems like this by restructuring the program. I'll give examples that work with multiple threads since those examples will also work single threaded.

Example 1: One thread (main thread) creates and maintains a linked list of items. Each item may or may not have a separate thread (item thread) that works on that object. When the item thread is done, it doesn't delete its associated item object. Instead, it sets a DeleteMe=true on that item. The main thread occasionally looks for items to delete on its linked list.

Example 2: One thread is adding items to a circular linked list. Another thread is processing items on that linked list. When an item gets processed, it's not deleted from the linked list. Instead, it's marked with UnsedItem=true and moves on to the next item. The thread that adds items to the circular linked list adds new nodes if the list isn't long enough. The list starts out circular with 3 unused nodes.

But I guess the simplest way to answer your question would be to use reference counting. You can have a Deleted bit on the object when you want to delete it. Other users of the object will remove their reference to it when they notice the Deleted bit. The object will be deleted when the ReferenceCount == 0. Only then, do you unload the dll.

You could create a linked list of pointers that are pointing to your object. Every time a new pointer points to the object, that pointer's address is added to your linked list. When the main object gets deleted, walk through the linked list setting all the pointers to that object to NULL. This is getting into some kind of primative garbage collection though.

I try to work around problems like this by restructuring the program. I'll give examples that work with multiple threads since those examples will also work single threaded.

  1. One thread (main thread) creates and maintains a linked list of items. Each item may or may not have a separate thread (item thread) that works on that object. When the item thread is done, it doesn't delete its associated item object. Instead, it sets a DeleteMe=true on that item. The main thread occasionally looks for items to delete on its linked list.

  2. One thread is adding items to a circular linked list. Another thread is processing items on that linked list. When an item gets processed, it's not deleted from the linked list. Instead, it's marked with UnsedItem=true and moves on to the next item. The thread that adds items to the circular linked list adds new nodes if the list isn't long enough. The list starts out circular with 3 unused nodes.

But I guess the simplest way to answer your question would be to use reference counting. You can have a Deleted bit on the object when you want to delete it. Other users of the object will remove their reference to it when they notice the Deleted bit. The object will be deleted when the ReferenceCount == 0. Only then, do you unload the dll.

You could create a linked list of pointers that are pointing to your object. Every time a new pointer points to the object, that pointer's address is added to your linked list. When the main object gets deleted, walk through the linked list setting all the pointers to that object to NULL. This is getting into some kind of primitive garbage collection though.

Source Link

I try to work around problems like this by restructuring the program. I'll give examples that work with multiple threads since those examples will also work single threaded.

Example 1: One thread (main thread) creates and maintains a linked list of items. Each item may or may not have a separate thread (item thread) that works on that object. When the item thread is done, it doesn't delete its associated item object. Instead, it sets a DeleteMe=true on that item. The main thread occasionally looks for items to delete on its linked list.

Example 2: One thread is adding items to a circular linked list. Another thread is processing items on that linked list. When an item gets processed, it's not deleted from the linked list. Instead, it's marked with UnsedItem=true and moves on to the next item. The thread that adds items to the circular linked list adds new nodes if the list isn't long enough. The list starts out circular with 3 unused nodes.

But I guess the simplest way to answer your question would be to use reference counting. You can have a Deleted bit on the object when you want to delete it. Other users of the object will remove their reference to it when they notice the Deleted bit. The object will be deleted when the ReferenceCount == 0. Only then, do you unload the dll.

You could create a linked list of pointers that are pointing to your object. Every time a new pointer points to the object, that pointer's address is added to your linked list. When the main object gets deleted, walk through the linked list setting all the pointers to that object to NULL. This is getting into some kind of primative garbage collection though.