When memory is freed
Objects are freed when their last reference goes. Break cycles, remove delegates and listeners, and close sessions yourself.
Objects are freed when the last reference to them goes: Lucent counts references, as Swift does. Most code never notices. Three cases need care.
Cycles are never freed
Section titled “Cycles are never freed”export class GraphNode { next: GraphNode | null = null;}
export function ring(): number { const a = new GraphNode(); const b = new GraphNode(); a.next = b; b.next = a; // a and b now keep each other alive const size = a.next === b ? 2 : 1; a.next = null; // break the cycle, so both can be freed return size;}Two objects that point at each other keep each other alive. Break the cycle when you're done: set one link to null, or keep such links in a Map you clear.
Delegates and listeners live until removed
Section titled “Delegates and listeners live until removed”const listeners = new Map<number, (tick: number) => void>();let nextId = 1;
export function subscribe(listener: (tick: number) => void): number { const id = nextId++; listeners.set(id, listener); return id;}
export function unsubscribe(id: number): void { listeners.delete(id); // the only way the listener is freed}An SDK object holds on to the delegate or listener you give it, and the listener often holds a JS callback. Give every subscribe an unsubscribe, and remove the SDK registration there too.
Debug builds log what's left when the app reloads: [lucent] 2 native reference(s) still held at teardown.
Close sessions and files yourself
Section titled “Close sessions and files yourself”export class LogWriter { private lines: string[] = []; private open = true;
write(line: string): void { if (!this.open) throw new Error("LogWriter is closed"); this.lines.push(line); }
close(): string[] { this.open = false; return this.lines; }}Give classes that hold a session, a file or a sensor a close() method, and call it from JavaScript in a finally. JavaScript's garbage collector decides when a JS reference goes, so don't rely on it to release native resources.
There's no Weak reference and no using statement yet (roadmap).