Running twice gives error
i've been trying to figure this out for a few days now, it's probably a memory leak, but i'm not sure.
i'm hoping someone can give it a look over and lead me down the right track.
heres the playground link:
https://playground.modular.com/user-redirect/?hubshare-preview=OTA0YjMzOWMzMjI5MzllNjQwYTUxYTgyYjg3M2YwMzFhNTVkMmQ5MzU3OWUxOWFiMmFkMGU0NmJlZDA5NWFhNi9VbmZvbGRlcjJlbGVjdHJpY2Jvb2dhbG9vLmlweW5i
55 Replies
sometimes, when i hold my tongue at the right angle, it gives the correct output for step [1,1,1], so i know the proccess is mostly working
It seems to me like you have a copy constructor and a move constructor. I don't see you using the ^ operator so you seem to be copying these a lot
You seem to be doing reference counting so I don't think that's a memory leak
but you array accessors don't seem to check the reference count
Are you intentionally trying to achieve shared mutable state here?
Because that can lead to very unpredictable behavior as you modify the same underlying storage in multiple places
Congrats @TeamPuzel, you just advanced to level 11!
what do you mean by this? i've tried doing some of the other stuff you mention
Well when you copy the array (and increase the reference count) you solve the problem of a double free.
But you still have the problem of shared mutable state, now you have 2 arrays that point to the same memory
if you try using both at once, like write something from one array into the other, but they both point to the same storage, you will be mutating an array while also reading from it
A copy must mean creating a new array.
what you can also do is copy-on-write, meaning you defer this operation to when trying to mutate an array, i.e. before writing to it your accessor should check if the reference count is greater than 1, if so copy
This is a danger in reference semantics based languages. The Objective-C array has a warning that you may not under any circumstances read and modify the same array if I recall correctly
when i do:
I don't think you're using this anywhere
Wouldn't you have to use the ^ operator to pass ownership?
I don't see you using that operator anywhere
i tried using that it doesnt do much if the variable isnt used afterwards anyway
I think if you don't use it it's going to pass a copy instead of consuming it
I would have to check though
if you had to guess, is the problem with the containers, or the graph?
Well you're saying it fails if you use it twice right
I would assume that's a memory bug with the array itself, since you're copying it
also depending on how i use it, it might fail
ok
Yeah that doesn't seem great
you could try checking for out of bounds access just in case too
unless you're doing that aready
oh yeah thats a good point, didnt even htink about that