M
Modularā€¢13mo ago
Helehex

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
No description
55 Replies
Helehex
Helehexā€¢13mo ago
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
No description
Helehex
Helehexā€¢13mo ago
TeamPuzel
TeamPuzelā€¢13mo ago
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
ModularBot
ModularBotā€¢13mo ago
Congrats @TeamPuzel, you just advanced to level 11!
Helehex
Helehexā€¢13mo ago
what do you mean by this? i've tried doing some of the other stuff you mention
TeamPuzel
TeamPuzelā€¢13mo ago
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
Helehex
Helehexā€¢13mo ago
when i do:
var arr = Array[Int](size)
arr = Array[Int](new_size, arr)

@always_inline
fn __init__(inout self, size: Int, owned array: Self): # owned array for the case of infinite self reference (self = array)
self._size = size
self._rc = Pointer[Int].alloc(1)
self._data = Pointer[T].alloc(size) # uses the size defined localy
self._rc.store(0)
self.copy(array)
self.clear(min(size, array._size), size)

@always_inline
fn copy(self, array: Self):
memcpy(self._data, array._data, min(self._size, array._size))
var arr = Array[Int](size)
arr = Array[Int](new_size, arr)

@always_inline
fn __init__(inout self, size: Int, owned array: Self): # owned array for the case of infinite self reference (self = array)
self._size = size
self._rc = Pointer[Int].alloc(1)
self._data = Pointer[T].alloc(size) # uses the size defined localy
self._rc.store(0)
self.copy(array)
self.clear(min(size, array._size), size)

@always_inline
fn copy(self, array: Self):
memcpy(self._data, array._data, min(self._size, array._size))
TeamPuzel
TeamPuzelā€¢13mo ago
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
Helehex
Helehexā€¢13mo ago
i tried using that it doesnt do much if the variable isnt used afterwards anyway
TeamPuzel
TeamPuzelā€¢13mo ago
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
Helehex
Helehexā€¢13mo ago
if you had to guess, is the problem with the containers, or the graph?
TeamPuzel
TeamPuzelā€¢13mo ago
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
Helehex
Helehexā€¢13mo ago
also depending on how i use it, it might fail ok
TeamPuzel
TeamPuzelā€¢13mo ago
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
Helehex
Helehexā€¢13mo ago
oh yeah thats a good point, didnt even htink about that
Want results from more Discord servers?
Add your server