When running a function as an alias and inside that function we modify a struct that modifies a dynamic vector it owns. And then we try to access the values of that dynamic vector at runtime the program crashes when running it a second time
Steps to reproduce
@value
struct Node(CollectionElement):
var id: Int
# var parents: DynamicVector[Int] # Having a Dynamicvector here causes error when compiling the program
var parents: StaticIntTuple[3] # using statictuples doesn't crash the program, so maybe the problem is using (for now at least) another dynamic memory, inside another struct?.
var operation: String
fn __init__(inout self, id: Int):
self.id = id
self.operation ="Input"
self.parents = StaticIntTuple[3](-1, -1, -1)
fn __init__(inout self, id: Int, operation: String):
self.id = id
self.operation = operation
self.parents = StaticIntTuple[3](-1, -1, -1)
@value
struct Graph:
var nodes: DynamicVector[Node]
var nodes_amount: Int
fn __init__(inout self):
self.nodes = DynamicVector[Node]()
self.nodes_amount = 0
fn __init__(inout self, inputs: Int):
self.nodes = DynamicVector[Node]()
self.nodes_amount = 0
# Doing an iteration to add the inputs inside the struct casues an error, I don't know why.
"""for i in range(inputs):
self.nodes.push_back(Node(self.nodes_amount))
self.nodes_amount += 1"""
fn add_node(inout self):
self.nodes.push_back(Node(self.nodes_amount))
self.nodes_amount += 1
fn add_node(inout self, node: Node):
self.nodes.push_back(node)
self.nodes_amount += 1
fn create_graph() -> Graph:
var g = Graph()
g.add_node()
# _ = g.nodes[0].operation + 2 # This crashes when compiling
return g
fn main():
alias graph = create_graph()
print(len(graph.nodes))
print(graph.nodes[0].operation)
# the program runs correctly the first time, but when running it a second time or more there is segmentation fault.
# And the problem seems to be when doing a pushback to the dynamic vector inside the Graph struct. So using a
# dynamic vector in an alias function doesn't cause this problem, but using a dynamic vector inside another struct causes a problem.
System information
- What OS did you do install Mojo on ? Pop.OS
- Provide version information for Mojo by pasting the output of `mojo -v`: mojo 0.7.0 (af002202)
- Provide Modular CLI version by pasting the output of `modular -v`: modular 0.4.1 (2d8afe15)
Bug description
When running a function as an alias and inside that function we modify a struct that modifies a dynamic vector it owns. And then we try to access the values of that dynamic vector at runtime the program crashes when running it a second time
Steps to reproduce
System information