digitalmars.D.learn - non-determinant object lifetime and memory management
- Frustrated (9/9) Nov 30 2013 I need to pass around some objects(specifically int[]) that may
- bearophile (6/15) Nov 30 2013 Your use case seems fit for using the GC.
- Rene Zwanenburg (4/13) Nov 30 2013 You can use the Array type in std.container [1]. It uses ref
- Frustrated (5/21) Nov 30 2013 How does it work? When you call clear it decrements the reference
- bioinfornatics (4/13) Nov 30 2013 Why you do not use one of this way:
- Frustrated (5/21) Dec 01 2013 It would seem if I am going to use some way it needs to be
I need to pass around some objects(specifically int[]) that may be used by several other objects at the same time. While I could clone these and free them when the parent object is done this wastes memory for no real reason except ease of use. Since many objects may contain a ptr to the array, what would be the best way to deal with deallocating them? I could wrap the array in a collection an use ARC but is there a better way? Is there something in std.allocators that can help? (Should be obvious that I'm trying to avoid the GC)
Nov 30 2013
Frustrated:I need to pass around some objects(specifically int[]) that may be used by several other objects at the same time. While I could clone these and free them when the parent object is done this wastes memory for no real reason except ease of use. Since many objects may contain a ptr to the array, what would be the best way to deal with deallocating them? I could wrap the array in a collection an use ARC but is there a better way? Is there something in std.allocators that can help? (Should be obvious that I'm trying to avoid the GC)Your use case seems fit for using the GC. Otherwise take a look at std.typecons.RefCounted, to be used in a wrapper that uses "alias this". Bye, bearophile
Nov 30 2013
On Saturday, 30 November 2013 at 08:35:23 UTC, Frustrated wrote:I need to pass around some objects(specifically int[]) that may be used by several other objects at the same time. While I could clone these and free them when the parent object is done this wastes memory for no real reason except ease of use. Since many objects may contain a ptr to the array, what would be the best way to deal with deallocating them? I could wrap the array in a collection an use ARC but is there a better way? Is there something in std.allocators that can help? (Should be obvious that I'm trying to avoid the GC)You can use the Array type in std.container [1]. It uses ref counting and the C heap internally.
Nov 30 2013
On Saturday, 30 November 2013 at 12:51:46 UTC, Rene Zwanenburg wrote:On Saturday, 30 November 2013 at 08:35:23 UTC, Frustrated wrote:How does it work? When you call clear it decrements the reference count and at 0 it free's the memory? That is the only self managed container in std.container?I need to pass around some objects(specifically int[]) that may be used by several other objects at the same time. While I could clone these and free them when the parent object is done this wastes memory for no real reason except ease of use. Since many objects may contain a ptr to the array, what would be the best way to deal with deallocating them? I could wrap the array in a collection an use ARC but is there a better way? Is there something in std.allocators that can help? (Should be obvious that I'm trying to avoid the GC)You can use the Array type in std.container [1]. It uses ref counting and the C heap internally.
Nov 30 2013
On Saturday, 30 November 2013 at 08:35:23 UTC, Frustrated wrote:I need to pass around some objects(specifically int[]) that may be used by several other objects at the same time. While I could clone these and free them when the parent object is done this wastes memory for no real reason except ease of use. Since many objects may contain a ptr to the array, what would be the best way to deal with deallocating them? I could wrap the array in a collection an use ARC but is there a better way? Is there something in std.allocators that can help? (Should be obvious that I'm trying to avoid the GC)Why you do not use one of this way: - const ref int[]… into function parameter - using shared/synchronized and ref to array
Nov 30 2013
On Sunday, 1 December 2013 at 02:29:42 UTC, bioinfornatics wrote:On Saturday, 30 November 2013 at 08:35:23 UTC, Frustrated wrote:It would seem if I am going to use some way it needs to be consistent. The first case would require creating []'s outside of the function which then doesn't solve the original problem. I'm not sure how the second case solves anything?I need to pass around some objects(specifically int[]) that may be used by several other objects at the same time. While I could clone these and free them when the parent object is done this wastes memory for no real reason except ease of use. Since many objects may contain a ptr to the array, what would be the best way to deal with deallocating them? I could wrap the array in a collection an use ARC but is there a better way? Is there something in std.allocators that can help? (Should be obvious that I'm trying to avoid the GC)Why you do not use one of this way: - const ref int[]… into function parameter - using shared/synchronized and ref to array
Dec 01 2013