This issue was quite a challenge to figure out from within my project’s code, but with persistence I reduced the compiler error down to a minimal repro. Follow the link below for the details in the issue opened today on Github. I do not understand the error message other than ‘something bad happened’.
Some kind of issue with memory allocation or a memory leak related to PythonObjects?
Error: field 'self.test_agent.name._buffer._len' destroyed out of the middle of a value, preventing the overall value from being destroyed
opened 07:19PM - 18 Mar 25 UTC
bug
mojo
max-repo
### Bug description
### Actual behavior
Referring to the code below, `struct Ap… pAgents:` emits the following compiler error at the assignment of `self.test_agent`:
`field 'self.test_agent.name._buffer._len' destroyed out of the middle of a value, preventing the overall value from being destroyed`
In `struct AgentBase`, if either instance variable of type PythonObject are removed completely or changed to a local variable scoped within `__init__`, the error goes away. Likewise, removing the instance variable `name` from `struct TestAgent` also prevents the error.
### Expected behavior
The code compiles and executes as written with no error.
### Steps to reproduce
**Minimum code to reproduce the error.**
```
from python import Python, PythonObject
trait TAgent(CollectionElement):
...
trait TAgent(CollectionElement):
...
@value
struct AgentBase[T: TAgent](CollectionElement):
var agent: T
var name: String
var py_datetime: PythonObject
var py_time: PythonObject
fn __init__(out self, name: String, agent: T) raises:
self.agent = agent
self.name = name
self.py_datetime = Python.import_module("datetime")
self.py_time = Python.import_module("time")
@value
struct TestAgent(TAgent):
var name: String
fn __init__(out self, name: String = "Bond, James Bond") raises:
self.name = name
@value
struct AppAgents:
var test_agent: AgentBase[TestAgent]
fn __init__(out self) raises:
alias AGENT_NAME = "007"
# Error on assignment statement.
# field 'self.test_agent.name._buffer._len' destroyed out of the middle of a value, preventing the overall value from being destroyed
self.test_agent = AgentBase(AGENT_NAME, TestAgent())
fn main() raises:
app_agents = AppAgents()
```
### System information
```
Package Version Build Size Kind Source
max 25.2.0.dev2025031805 release 9.7 KiB conda max
max-core 25.2.0.dev2025031805 release 251.6 MiB conda max-core
max-python 25.2.0.dev2025031805 release 120.3 MiB conda max-python
```
```
Magic version: 0.7.2
System
------------
Pixi version: 0.41.4
Platform: linux-64
Virtual packages: __unix=0=0
: __linux=6.11.0=0
: __glibc=2.40=0
: __cuda=12.6=0
: __archspec=1=skylake
Cache dir: /home/johnsoe1/.cache/rattler/cache
Auth storage: /home/johnsoe1/.rattler/credentials.json
Config locations: No config files found
Global
------------
Bin dir: /home/johnsoe1/.modular/bin
Environment dir: /home/johnsoe1/.modular/envs
Manifest dir: /home/johnsoe1/.modular/manifests/pixi-global.toml
```
2 Likes