Atapt Node and GraphView class to cyclic graph
This issue is a more focused discussion about a specific memory leak among those presented in aidge#195
Context
- Cyclic graph -> indirect leak due to
shared_ptrreference between Nodes.
Current reference system
%%{init: {'flowchart': { 'curve': 'monotoneY'}, 'fontFamily': 'Verdana' } }%%
flowchart TB
ConvOp{{"Conv<br/>(Operator)"}}:::earth_yellow
ConvNode(("Conv<br/>(Node)")):::earth_blue
WeightOp{{"Producer<br/>(Operator)"}}:::earth_yellow
WeightNode(("Producer<br/>(Node)")):::earth_blue
BiasOp{{"Producer<br/>(Operator)"}}:::earth_yellow
BiasNode(("Producer<br/>(Node)")):::earth_blue
subgraph "User Scope"
ConvNode
end
ConvNode --> ConvOp
WeightNode --> WeightOp
BiasNode --> BiasOp
WeightNode & BiasNode -..->|"weak_ptr"| ConvNode
ConvNode --->|"shared_ptr"| WeightNode & BiasNode
classDef earth_yellow fill:#F0D760,stroke:#333,stroke-width:2px;
classDef earth_blue fill:#B3E5FC,stroke:#333,stroke-width:2px;
linkStyle 3,4 stroke:#000,stroke-width:2px,stroke-dasharray: 5 5;
linkStyle 0,1,2,5,6 stroke:#ff0000,stroke-width:2px;
Solution
- Nodes reference each other with a
weak_ptrconnection (there is no real choice for this...) - a global view references each created Node. This view is not known to the user. The purpose of such view is to prevent Nodes not referenced in the local scope from being deleted even though they are used (like Producers created for Conv)
When a Node is removed from a GraphView, if this Node has no other connection than the one to the GlobalView, it is deleted. Of course if the Node is in the current context, there is at least one reference other than the one from the GlobalView so it won't be deleted.
If a Nodes becomes unused in a function but is still referenced in the context of this function, I am not sure how to delete this Node when exiting the function.
Here would be the connections:
%%{init: {'flowchart': { 'curve': 'monotoneY'}, 'fontFamily': 'Verdana' } }%%
flowchart LR
ConvOp{{"Conv<br/>(Operator)"}}:::earth_yellow
ConvNode(("Conv<br/>(Node)")):::earth_blue
WeightOp{{"Producer<br/>(Operator)"}}:::earth_yellow
WeightNode(("Producer<br/>(Node)")):::earth_blue
BiasOp{{"Producer<br/>(Operator)"}}:::earth_yellow
BiasNode(("Producer<br/>(Node)")):::earth_blue
subgraph "GlobalView"
subgraph "User Scope"
ConvNode
end
WeightNode
BiasNode
end
ConvNode --> ConvOp
WeightNode --> WeightOp
BiasNode --> BiasOp
WeightNode & BiasNode -..-> ConvNode
ConvNode <-..- WeightNode & BiasNode
classDef earth_yellow fill:#F0D760,stroke:#333,stroke-width:2px;
classDef earth_blue fill:#B3E5FC,stroke:#333,stroke-width:2px;
linkStyle 3,4,5,6 stroke:#000,stroke-width:2px,stroke-dasharray: 5 5;
linkStyle 0,1,2 stroke:#ff0000,stroke-width:2px;
Edited by Maxence Naud