-
So as the title asks does mojo guarantee that the destructor is always called when a value goes out of scope? I was wondering about this from the programming manual
I came across some interesting reading about rust and some issues in creating async structured concurrency. At least part of the problem (amongst others), is that rust does not guarantee calling its This non-guarantee of a destructor being called has had some profound impact on rust's type system and language design. The infamous rust leakpocalypse was discovered days before rust's 1.0 release, and there was some discussion about a My understanding is that a lot of this could have been avoided if destructor calls were guaranteed to run rather than basically leaking memory (which is not considered Undefined Behavior). But I also wonder...is it even possible to guarantee? For example, how would you handle FFI where the foreign language will be managing the memory? This would include syscalls to the OS, so it seems like the ability to purposefully leak memory is required. And if that is the case, then you run into the rust dilemma about a |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I guess scope is the wrong word here since mojo seems to call the destructor at its last usage rather than the end of a scope, unless there are still references to it. So perhaps the question should be read as will the destructor always be called at a variable's last use? |
Beta Was this translation helpful? Give feedback.
-
Sorry I didn't see this earlier. Mojo has more aggressive policy than Rust that I call "ASAP" destruction - it doesn't wait until end of scope. Check out this link for more details: https://docs.modular.com/mojo/programming-manual.html#behavior-of-destructors |
Beta Was this translation helpful? Give feedback.
-
Of course, you can also just call malloc and free from Mojo and ignore "smart pointers" etc. If you do that, safety is in your own hands :) |
Beta Was this translation helpful? Give feedback.
We don't have answers to these questions, but I can give you MHO. These aren't "team" or "modular" opinions, just based on my experience of where I guess things will go:
Rust made a bunch of very specific an intertwined decisions that Mojo is not making, so while I can see that you're concerned about this, I wouldn't worry much about it.
Python doesn't even have access control (public/private) so we have a lot more fundamental things to work throu…