digitalmars.D.learn - GC Destruction Order
- bitwise (15/15) May 19 2015 In C#, it's possible that class members can actually be destroyed before...
- Adam D. Ruppe (3/4) May 19 2015 Yes. The GC considers all the unreferenced memory dead at the
- bitwise (6/10) May 19 2015 Ugh... I was really hoping D had something better up it's sleeve.
- Steven Schveighoffer (9/19) May 19 2015 It's actually quite impossible for the GC to know what pointers are
- bitwise (4/25) May 19 2015 Ok, thanks for the quick answers =D
- rsw0x (4/14) May 19 2015 It actually does, check out RefCounted!T and Unique!T in
- Namespace (2/18) May 19 2015 By the way: when is 2.068 released?
- rsw0x (3/22) May 19 2015 "After dconf"
- Namespace (2/25) May 19 2015 I thought the new releases would come faster.
- Steven Schveighoffer (3/8) May 19 2015 They should. This is an exception. Read the thread quoted above.
- bitwise (21/34) May 19 2015 Any idea what the plans are?. Does RefCounted become thread safe?
- rsw0x (12/38) May 19 2015 I don't understand what you're asking here. If you hold a
- bitwise (4/22) May 19 2015 Thanks for confirming, but given your apparent tendency toward pinhole
- Steven Schveighoffer (7/31) May 19 2015 With the current GC, yes. RefCounted needs to be thread safe in order to...
- bitwise (9/46) May 19 2015 This seems like it could result in some destructors being delayed
- bitwise (4/9) May 19 2015 ....which is why I am asking if there are any plans to implement somethi...
- Adam D. Ruppe (5/7) May 19 2015 At the top:
- bitwise (17/24) May 19 2015 Thanks!
- Kagamin (7/12) May 20 2015 And what you're asking. Just for the record: C++ memory
- bitwise (7/21) May 20 2015 Yes, but D claims to support manual memory management. It seems
- Kagamin (11/24) May 20 2015 It's WIP. There were thoughts to run finalizers on the thread
- Steven Schveighoffer (12/17) May 21 2015 It's essential for lockless thread-local programming.
- Kagamin (3/8) May 21 2015 You mean the reference counting part?
- Steven Schveighoffer (12/18) May 21 2015 Yes, if you put a File as a class member, the destructor of the class
- Kagamin (5/10) May 21 2015 Just for the record: .net doesn't use reference counting for
- Steven Schveighoffer (15/62) May 21 2015 OK, I see your point. Yes, you need @nogc to not leak.
the containing object. Example: class Stuff { Class1 thing1; Class2 thing2; ~Stuff() { thing1.DoSomeFinalization(); // [1] } } I forget what the exact behavior was, but basically, [1] is unsafe because it may have already been destructed/freed by the time ~Stuff() is called. Is this also true for D? Bit
May 19 2015
On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.
May 19 2015
On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve. I have heard about attempts to add precise GC to D though... would precise GC address this problem in some way? BitIs this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.
May 19 2015
On 5/19/15 2:37 PM, bitwise wrote:On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It's actually quite impossible for the GC to know what pointers are "owning" pointers and what ones are not. And you could never have ownership cycles. You could use some version of malloc/free to do it. But you have to take care of GC references inside that malloc'd block.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.I have heard about attempts to add precise GC to D though... would precise GC address this problem in some way?No. Precise scanning just (potentially) cuts down on scanning time, and avoids false pointers. -Steve
May 19 2015
On Tue, 19 May 2015 14:55:55 -0400, Steven Schveighoffer <schveiguy yahoo.com> wrote:On 5/19/15 2:37 PM, bitwise wrote:Ok, thanks for the quick answers =D BitOn Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It's actually quite impossible for the GC to know what pointers are "owning" pointers and what ones are not. And you could never have ownership cycles. You could use some version of malloc/free to do it. But you have to take care of GC references inside that malloc'd block.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.I have heard about attempts to add precise GC to D though... would precise GC address this problem in some way?No. Precise scanning just (potentially) cuts down on scanning time, and avoids false pointers. -Steve
May 19 2015
On Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.
May 19 2015
On Tuesday, 19 May 2015 at 19:36:23 UTC, rsw0x wrote:On Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:By the way: when is 2.068 released?On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.
May 19 2015
On Tuesday, 19 May 2015 at 19:45:38 UTC, Namespace wrote:On Tuesday, 19 May 2015 at 19:36:23 UTC, rsw0x wrote:"After dconf" http://forum.dlang.org/thread/5554D763.1080308 dawg.eu#post-5554D763.1080308:40dawg.euOn Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:By the way: when is 2.068 released?On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.
May 19 2015
On Tuesday, 19 May 2015 at 20:02:07 UTC, rsw0x wrote:On Tuesday, 19 May 2015 at 19:45:38 UTC, Namespace wrote:I thought the new releases would come faster.On Tuesday, 19 May 2015 at 19:36:23 UTC, rsw0x wrote:"After dconf" http://forum.dlang.org/thread/5554D763.1080308 dawg.eu#post-5554D763.1080308:40dawg.euOn Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:By the way: when is 2.068 released?On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.
May 19 2015
On 5/19/15 4:16 PM, Namespace wrote:On Tuesday, 19 May 2015 at 20:02:07 UTC, rsw0x wrote:They should. This is an exception. Read the thread quoted above. -Steve"After dconf" http://forum.dlang.org/thread/5554D763.1080308 dawg.eu#post-5554D763.1080308:40dawg.euI thought the new releases would come faster.
May 19 2015
On Tue, 19 May 2015 15:36:21 -0400, rsw0x <anonymous anonymous.com> wrote:On Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:Any idea what the plans are?. Does RefCounted become thread safe? Correct me if I'm wrong though, but even if RefCounted itself was thread-safe, RefCounted objects could still be placed in classes, at which point you might as well use a GC'ed class instead, because you'd be back to square-one with your destructor racing around on some random thread. I'm finding it hard to be optimistic about the memory model of D. The idea of marking absolutely everything in your program with " nogc" just to make it safe is ludicrous. Something like this would be a little more reasonable, but I see no discussions about it: nogc module my_module; or nogc class Something{} DIP74 seems like it would improve the situation a lot, but wouldn't work as expected as long as any other class that may contain it could be GC'ed. This also seems like a monumental undertaking that won't actually be implemented for years, if at all. I'm hoping someone will correct me here, because other than the memory model, D seems like a very well designed language. BitOn Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.
May 19 2015
On Tuesday, 19 May 2015 at 21:07:52 UTC, bitwise wrote:On Tue, 19 May 2015 15:36:21 -0400, rsw0x <anonymous anonymous.com> wrote:I don't understand what you're asking here. If you hold a RefCounted resource in a GC managed object, yes, it will be tied to the GC object's lifetime. With your avoidance of the GC, I feel like you were lied to by a C++ programmer that reference counting is the way to do all memory management, when in reality reference counting is dog slow and destroys your cache locality(esp. without compiler support.) Reference counting is meant to be used where you need absolute control over a resource's lifetime(IMHO,) not as a general purpose memory management tool. Bye.On Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:Any idea what the plans are?. Does RefCounted become thread safe? Correct me if I'm wrong though, but even if RefCounted itself was thread-safe, RefCounted objects could still be placed in classes, at which point you might as well use a GC'ed class instead, because you'd be back to square-one with your destructor racing around on some random thread.On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.
May 19 2015
On Tue, 19 May 2015 17:52:36 -0400, rsw0x <anonymous anonymous.com> wrote:On Tuesday, 19 May 2015 at 21:07:52 UTC, bitwise wrote:Thanks for confirming, but given your apparent tendency toward pinhole view points, it's unsurprising that you don't understand what I'm asking. BitAny idea what the plans are?. Does RefCounted become thread safe? Correct me if I'm wrong though, but even if RefCounted itself was thread-safe, RefCounted objects could still be placed in classes, at which point you might as well use a GC'ed class instead, because you'd be back to square-one with your destructor racing around on some random thread.I don't understand what you're asking here. If you hold a RefCounted resource in a GC managed object, yes, it will be tied to the GC object's lifetime. With your avoidance of the GC, I feel like you were lied to by a C++ programmer that reference counting is the way to do all memory management, when in reality reference counting is dog slow and destroys your cache locality(esp. without compiler support.) Reference counting is meant to be used where you need absolute control over a resource's lifetime(IMHO,) not as a general purpose memory management tool.
May 19 2015
On 5/19/15 5:07 PM, bitwise wrote:On Tue, 19 May 2015 15:36:21 -0400, rsw0x <anonymous anonymous.com> wrote:With the current GC, yes. RefCounted needs to be thread safe in order to use it. But if we change the GC, we could ensure destructors are only called in the thread they were created in (simply defer destructors until the next GC call in that thread).On Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:Any idea what the plans are?. Does RefCounted become thread safe? Correct me if I'm wrong though, but even if RefCounted itself was thread-safe, RefCounted objects could still be placed in classes, at which point you might as well use a GC'ed class instead, because you'd be back to square-one with your destructor racing around on some random thread.On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.I'm finding it hard to be optimistic about the memory model of D. The idea of marking absolutely everything in your program with " nogc" just to make it safe is ludicrous.That makes no sense, the GC is not unsafe. -Steve
May 19 2015
On Tue, 19 May 2015 18:47:26 -0400, Steven Schveighoffer <schveiguy yahoo.com> wrote:On 5/19/15 5:07 PM, bitwise wrote:This seems like it could result in some destructors being delayed indefinitely.On Tue, 19 May 2015 15:36:21 -0400, rsw0x <anonymous anonymous.com> wrote:With the current GC, yes. RefCounted needs to be thread safe in order to use it. But if we change the GC, we could ensure destructors are only called in the thread they were created in (simply defer destructors until the next GC call in that thread).On Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:Any idea what the plans are?. Does RefCounted become thread safe? Correct me if I'm wrong though, but even if RefCounted itself was thread-safe, RefCounted objects could still be placed in classes, at which point you might as well use a GC'ed class instead, because you'd be back to square-one with your destructor racing around on some random thread.On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.Maybe I worded that incorrectly, but my point is that when you're running with the GC disabled, you should only use methods marked with nogc if you want to make sure your code doesn't leak right? that's a lot of attributes O_O BitI'm finding it hard to be optimistic about the memory model of D. The idea of marking absolutely everything in your program with " nogc" just to make it safe is ludicrous.That makes no sense, the GC is not unsafe. -Steve
May 19 2015
On Tue, 19 May 2015 19:03:02 -0400, bitwise <bitwise.pvt gmail.com> wrote:Maybe I worded that incorrectly, but my point is that when you're running with the GC disabled, you should only use methods marked with nogc if you want to make sure your code doesn't leak right? that's a lot of attributes O_O Bit....which is why I am asking if there are any plans to implement something like nogc for entire modules or classes. Bit
May 19 2015
On Tuesday, 19 May 2015 at 23:10:21 UTC, bitwise wrote:....which is why I am asking if there are any plans to implement something like nogc for entire modules or classes.At the top: nogc: <stuff here> Gotta do it inside the class too i think.
May 19 2015
On Tue, 19 May 2015 19:16:14 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:On Tuesday, 19 May 2015 at 23:10:21 UTC, bitwise wrote:Thanks! this seems to work too: nogc { <stuff> } I think this is still a problem though: struct StackThing { ~this() { writeln("where am I?"); } } class HeapThing{ StackThing thing; } HeapThing thing = new HeapThing(); Basically, I can't design a struct and be sure the destructor will be called on the same thread as where it went out of scope. I hope I'm wrong and DIP74 comes soon =/ Bit....which is why I am asking if there are any plans to implement something like nogc for entire modules or classes.At the top: nogc: <stuff here> Gotta do it inside the class too i think.
May 19 2015
On Tuesday, 19 May 2015 at 22:15:18 UTC, bitwise wrote:Thanks for confirming, but given your apparent tendency toward pinhole view points, it's unsurprising that you don't understand what I'm asking.And what you're asking. Just for the record: C++ memory management techniques are not designed to work in GC environment. On Wednesday, 20 May 2015 at 03:44:58 UTC, bitwise wrote:Basically, I can't design a struct and be sure the destructor will be called on the same thread as where it went out of scope.If your resource finalization code has some specific threading requirements, you implement those yourself in a way your code requires it. Or instead freeing resources normally in due time.
May 20 2015
On Wednesday, 20 May 2015 at 08:01:46 UTC, Kagamin wrote:On Tuesday, 19 May 2015 at 22:15:18 UTC, bitwise wrote:Yes, but D claims to support manual memory management. It seems to get second class treatment though. I'm pretty sure I canThanks for confirming, but given your apparent tendency toward pinhole view points, it's unsurprising that you don't understand what I'm asking.And what you're asking. Just for the record: C++ memory management techniques are not designed to work in GC environment.On Wednesday, 20 May 2015 at 03:44:58 UTC, bitwise wrote:AFAIK D does not provide any built in functionality like Objective-C's 'runOnMainThread', which makes this a painful option.Basically, I can't design a struct and be sure the destructor will be called on the same thread as where it went out of scope.If your resource finalization code has some specific threading requirements, you implement those yourself in a way your code requires it. Or instead freeing resources normally in due time.
May 20 2015
On Wednesday, 20 May 2015 at 13:54:29 UTC, bitwise wrote:Yes, but D claims to support manual memory management. It seems to get second class treatment though.It's WIP. There were thoughts to run finalizers on the thread where the object was allocated (I doubt it's a good idea, though). Anyway, if you're doing manual memory management, how GC popped up? If you have your manual memory managed with GC, it means you have a memory leak: manually managed memory shouldn't become garbage without being freed. I suppose it will be a long way before D rediscovers .net practices.I use Marshal.AllocHGlobal.You asked for destructor being called on the thread where it went out of scope, which is not necessarily the main thread.AFAIK D does not provide any built in functionality like Objective-C's 'runOnMainThread', which makes this a painful option.Basically, I can't design a struct and be sure the destructor will be called on the same thread as where it went out of scope.If your resource finalization code has some specific threading requirements, you implement those yourself in a way your code requires it. Or instead freeing resources normally in due time.
May 20 2015
On 5/20/15 11:09 AM, Kagamin wrote:On Wednesday, 20 May 2015 at 13:54:29 UTC, bitwise wrote:It's essential for lockless thread-local programming. At this moment, a thread-local-only heap pointer must deal with multi-threading issues simply because destructors can run on another thread, even though the reference is thread-local. The biggest example right now is reference-counted structures such as std.stdio.File. The absolute best part about the shared qualifier is the lack of shared qualifier -- you can be certain something isn't shared if it doesn't have shared attached to it. Right now, even in that case, you still have to worry about RAII objects being destroyed in other threads. We shouldn't have to worry about that. -SteveYes, but D claims to support manual memory management. It seems to get second class treatment though.It's WIP. There were thoughts to run finalizers on the thread where the object was allocated (I doubt it's a good idea, though).
May 21 2015
On Thursday, 21 May 2015 at 12:33:33 UTC, Steven Schveighoffer wrote:At this moment, a thread-local-only heap pointer must deal with multi-threading issues simply because destructors can run on another thread, even though the reference is thread-local. The biggest example right now is reference-counted structures such as std.stdio.File.You mean the reference counting part?
May 21 2015
On 5/21/15 8:40 AM, Kagamin wrote:On Thursday, 21 May 2015 at 12:33:33 UTC, Steven Schveighoffer wrote:Yes, if you put a File as a class member, the destructor of the class would call the File's destructor, which could be run in any thread. Since File is a reference counted wrapper, the destructor decrements the reference count and possibly closes the file. The whole operation must be atomic if we are spread across threads. But if you only ever access that containing class in a local thread, you are paying the cost of locking (or at least atomic decrement) for all reference counts on that file, for very little reason. Especially if the File doesn't live in a heap object (dtor doesn't know where it's being called from). -SteveAt this moment, a thread-local-only heap pointer must deal with multi-threading issues simply because destructors can run on another thread, even though the reference is thread-local. The biggest example right now is reference-counted structures such as std.stdio.File.You mean the reference counting part?
May 21 2015
On Thursday, 21 May 2015 at 12:33:33 UTC, Steven Schveighoffer wrote:At this moment, a thread-local-only heap pointer must deal with multi-threading issues simply because destructors can run on another thread, even though the reference is thread-local. The biggest example right now is reference-counted structures such as std.stdio.File.Just for the record: .net doesn't use reference counting for system resources like files, it works fine, such resources usually have well-defined ownership.
May 21 2015
On 5/19/15 7:03 PM, bitwise wrote:On Tue, 19 May 2015 18:47:26 -0400, Steven Schveighoffer <schveiguy yahoo.com> wrote:That's already the case.On 5/19/15 5:07 PM, bitwise wrote:This seems like it could result in some destructors being delayed indefinitely.On Tue, 19 May 2015 15:36:21 -0400, rsw0x <anonymous anonymous.com> wrote:With the current GC, yes. RefCounted needs to be thread safe in order to use it. But if we change the GC, we could ensure destructors are only called in the thread they were created in (simply defer destructors until the next GC call in that thread).On Tuesday, 19 May 2015 at 18:37:31 UTC, bitwise wrote:Any idea what the plans are?. Does RefCounted become thread safe? Correct me if I'm wrong though, but even if RefCounted itself was thread-safe, RefCounted objects could still be placed in classes, at which point you might as well use a GC'ed class instead, because you'd be back to square-one with your destructor racing around on some random thread.On Tue, 19 May 2015 14:19:30 -0400, Adam D. Ruppe <destructionator gmail.com> wrote:It actually does, check out RefCounted!T and Unique!T in std.typecons. They're sort of limited right now but undergoing a major revamp in 2.068.On Tuesday, 19 May 2015 at 18:15:06 UTC, bitwise wrote:Ugh... I was really hoping D had something better up it's sleeve.Is this also true for D?Yes. The GC considers all the unreferenced memory dead at the same time and may clean up the class and its members in any order.OK, I see your point. Yes, you need nogc to not leak. The point of nogc was to ensure machine-checkable prevention of GC calls. The idea is to put nogc on main(), and then all your calls will have to be nogc. Where it absolutely comes in handy is compiler generated GC calls that can be unexpected (e.g. closures). But I'd still recommend not disabling the GC, as that is redundant, and having a stray GC call will not leak if something somehow (roguely) uses the GC (there are ways to do this). Alternatively, you can run a collection at opportune times just in case. It means you have to live with a subset of the language, and phobos cannot support you 100%. We are working to ensure that it's nogc as much as possible. -SteveMaybe I worded that incorrectly, but my point is that when you're running with the GC disabled, you should only use methods marked with nogc if you want to make sure your code doesn't leak right? that's a lot of attributes O_OI'm finding it hard to be optimistic about the memory model of D. The idea of marking absolutely everything in your program with " nogc" just to make it safe is ludicrous.That makes no sense, the GC is not unsafe.
May 21 2015