Program crash: GC destroys an object unexpectedly

Steven Schveighoffer schveiguy at gmail.com
Wed Sep 22 18:38:34 UTC 2021


On 9/22/21 11:47 AM, eugene wrote:
> On Wednesday, 22 September 2021 at 12:26:53 UTC, Steven Schveighoffer 
> wrote:
>> On 9/22/21 8:22 AM, eugene wrote:
>>> And it follows that programming in GC-supporting languages
>>> *may* be harder than in languages with manual memory
>>> management, right?
> 
> I meant my this particular trouble...

In terms of any kind of memory management, whether it be ARC, manual, 
GC, or anything else, there will always be pitfalls. It's just that you 
have to get used to the pitfalls and how to avoid them.

I could see a person used to GC complaining that C requires you to free 
every pointer *exactly once*. I mean, how can that be acceptable? ;)

> I do not want to understand how and what
> compiler generates, I just want to
> get working program without any oddities.

And for the most part, you do not. It's just when you travel outside the 
language, you must obey certain constraints. Those constraints are laid 
out, and unfortunately not exactly correct (we need to amend the 
spec/library for this), but given correct constraints, the rules are not 
super-difficult to follow.

> Nevertheless, thank you again for your nice explanation!

You are welcome!

>> It's telling that I've been using D for 14 years and never had or seen 
>> this problem.
> 
> Bond. James Bond. :) 25 years of C coding.
> Now, imaging a shock I was under when
> I 'discovered' that swapping two lines of code
> can magically fix my prog and make GC do
> the right thing. :)

I'm right there with you (been writing code for about 25 years, maybe 
26, depending on when I switched majors to CS in college).

But realize that C has it's share of "shocks" as well, you are just more 
used to them (or maybe you have been lucky so far?)

> 
> Actually, D is a nice language per se
> and I truly wish it to be as popular as java/python/etc.
> But these GC ... mmm... 'features' may reduce
> to zero any wish to learn D, that's about it.

Your experience is not typical though (clearly, as many of us long-time 
D users had no idea why it was happening). But for sure if this turns 
you off, I can understand how it can be too frustrating to learn the new 
rules. I personally would probably never write C code again if I can 
help it, despite having decades of experience in C/C++. I did recently 
have to port a C plugin library from PHP 5 to PHP 7, and it wasn't pleasant.

> 
> When my C program crashes, I'm 100% sure I made something stupid
> 
> - forget to initialize a pointer, easy to find and fix
> - did some memory corruption (worse, but then electric fence is my best 
> friend)
> 
> But if a crash is caused by 'optimization' + GC...
> It looks like a programmer must keep some
> implicit/unwritten rules in order to write correctly...
> 

I find it interesting how you blame yourself for C's idiosyncrasies, but 
not for D's ;) I would say C has far more pitfalls than D. Check out the 
undefined behaviors for C.

-Steve


More information about the Digitalmars-d-learn mailing list