-1
The Myth of Smart Pointers (www.logikalsolutions.com)
you are viewing a single comment's thread
view the rest of the comments
[-] lysdexic@programming.dev 12 points 11 months ago

This blog post writes a dissertation about garbage collection, heap memory management, the absolute need to take courses on assembly language, and other contrived and absurd tangents.

Looking at the code, the guy gets a double-free because he instantiates two std::unique_ptr from the same raw pointer.

I'm sure the author felt very clever to pull up all these topics to write a blog post about, but in the end all they're doing is writing buggy code based on their misconception of a topic.

[-] AlkaliMarxist@hexbear.net 3 points 11 months ago

Yeah, unless I'm missing something the author would have the same outcome with regular pointers if he'd freed them at the same time (one at the end of the anon scope and one at the end of fun1). This is nothing to do with garbage collection and is simply a result of, as you mention, pointing to the same memory with two pointers and freeing both.

His issue seems to be with the implementation of malloc, which is pretty funny because he's basically claiming C itself has unpredictable garbage collection. I almost can't believe it's legit, it seems like such a basic, fundamental misunderstanding of concepts it's like chatGPT output.

[-] lysdexic@programming.dev 2 points 11 months ago

Yeah, unless I’m missing something the author would have the same outcome with regular pointers if he’d freed them at the same time (one at the end of the anon scope and one at the end of fun1).

That's basically it. The way the blogger wrote fun1 means the pointer is freed once the function exits, because they explicitly added the std::unique_ptr to take over its lifetime. Afterwards they are surprised by the fact that it really took over its lifetime.

The weird part is that the blogger had to go way out of its way to write that bug.

I almost can’t believe it’s legit, it seems like such a basic, fundamental misunderstanding of concepts it’s like chatGPT output.

I agree. It almost sounds like one of those coding exercises recruiters throw candidates in preliminary hiring rounds to weed out the bottom 5% that have no idea what they are doing.

load more comments (20 replies)
this post was submitted on 30 Nov 2023
-1 points (45.5% liked)

C++

1732 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS