digitalmars.D - Should struct destructors be required to be nogc?
- bachmeier (10/10) Sep 13 2023 I recently had an invalid memory operation error, and given how
- Jonathan M Davis (12/22) Sep 13 2023 Well, if the class or struct is on the stack, being @nogc should be
- Commander Zot (5/19) Sep 14 2023 i'd prefer: don't call destructors from the GC at all, it was
- deadalnix (8/11) Sep 15 2023 I don't see why that should be a problem at all. Java, C# and
- Jonathan M Davis (10/21) Sep 15 2023 I don't know enough about what D's GC is doing to know why it can't hand...
- Steven Schveighoffer (4/14) Sep 14 2023 Since 2.102 you should get a stack trace now. Did that not happen
- bachmeier (6/9) Sep 14 2023 I'm using the LDC package for Ubuntu 22.04, which is LDC 1.28,
I recently had an invalid memory operation error, and given how vague that message is, I had no idea where to look for the problem. The problem was string concatenation in a struct destructor. My feeling is that destructors should always be nogc, because if it's not, you've done something wrong and your program might unexpectedly crash. After solving the problem, I came upon [this page](https://wiki.dlang.org/InvalidMemoryOperationError). If the error message had at least included a link to that page, it would have saved me a lot of time.
Sep 13 2023
On Wednesday, September 13, 2023 7:39:25 PM MDT bachmeier via Digitalmars-d wrote:I recently had an invalid memory operation error, and given how vague that message is, I had no idea where to look for the problem. The problem was string concatenation in a struct destructor. My feeling is that destructors should always be nogc, because if it's not, you've done something wrong and your program might unexpectedly crash. After solving the problem, I came upon [this page](https://wiki.dlang.org/InvalidMemoryOperationError). If the error message had at least included a link to that page, it would have saved me a lot of time.Well, if the class or struct is on the stack, being nogc should be unnecessary (the same if you're using an allocator other than the GC allocator), because it won't be run when the GC is running a collection. So, arguably, requiring nogc would be too restrictive for some use cases. However, it's also true that they could just as easily be on the heap and run into a similar issue (particularly with classes, since they almost always live on the GC heap). So, an nogc requirement would likely make sense for most use cases, but I don't know how reasonable it is to absolutely require it. - Jonathan M Davis
Sep 13 2023
On Thursday, 14 September 2023 at 03:03:38 UTC, Jonathan M Davis wrote:On Wednesday, September 13, 2023 7:39:25 PM MDT bachmeier via Digitalmars-d wrote:i'd prefer: don't call destructors from the GC at all, it was never guaranteed anyway as far as i know. introduce finalizers, which are _only_ called by the GC and are nogc.[...]Well, if the class or struct is on the stack, being nogc should be unnecessary (the same if you're using an allocator other than the GC allocator), because it won't be run when the GC is running a collection. So, arguably, requiring nogc would be too restrictive for some use cases. However, it's also true that they could just as easily be on the heap and run into a similar issue (particularly with classes, since they almost always live on the GC heap). So, an nogc requirement would likely make sense for most use cases, but I don't know how reasonable it is to absolutely require it. - Jonathan M Davis
Sep 14 2023
On Thursday, 14 September 2023 at 09:29:21 UTC, Commander Zot wrote:i'd prefer: don't call destructors from the GC at all, it was never guaranteed anyway as far as i know. introduce finalizers, which are _only_ called by the GC and are nogc.friend all support allocation during finalization and even object resurrection. At some point, we got to stop chasing the new trendy feature, and make sure the one we have work properly up to the standard a modern dev can expect.
Sep 15 2023
On Friday, September 15, 2023 5:32:00 PM MDT deadalnix via Digitalmars-d wrote:On Thursday, 14 September 2023 at 09:29:21 UTC, Commander Zot wrote:I don't know enough about what D's GC is doing to know why it can't handle this (or if I knew, I forgot), but certainly, I would agree that ideally, the GC would just be able to handle having stuff allocated while a destructor is running. And unless there's something fundamental preventing it in D's case, it would definitely be a better solution than disallowing allocating in destructors, much as needing to allocate in destructorss should be pretty rare. - Jonathan M Davisi'd prefer: don't call destructors from the GC at all, it was never guaranteed anyway as far as i know. introduce finalizers, which are _only_ called by the GC and are nogc.friend all support allocation during finalization and even object resurrection. At some point, we got to stop chasing the new trendy feature, and make sure the one we have work properly up to the standard a modern dev can expect.
Sep 15 2023
On Thursday, 14 September 2023 at 01:39:25 UTC, bachmeier wrote:I recently had an invalid memory operation error, and given how vague that message is, I had no idea where to look for the problem. The problem was string concatenation in a struct destructor. My feeling is that destructors should always be nogc, because if it's not, you've done something wrong and your program might unexpectedly crash. After solving the problem, I came upon [this page](https://wiki.dlang.org/InvalidMemoryOperationError). If the error message had at least included a link to that page, it would have saved me a lot of time.Since 2.102 you should get a stack trace now. Did that not happen for you? -Steve
Sep 14 2023
On Thursday, 14 September 2023 at 08:31:05 UTC, Steven Schveighoffer wrote:Since 2.102 you should get a stack trace now. Did that not happen for you? -SteveI'm using the LDC package for Ubuntu 22.04, which is LDC 1.28, based on 2.098. That might have helped. I think in this case, though, the compiler should have caught the problem and refused to create the binary.
Sep 14 2023