@@ -60,8 +60,9 @@ pub fn handleOpenCommand(allocator: std.mem.Allocator, writer: std.io.AnyWriter,
6060    }
6161
6262    const  temp_path  =  try  file_manager .createTempFile (allocator , config .base_dir , hostname , remote_path );
63+     errdefer  allocator .free (temp_path );
6364
64-     // Determine  initial content and write once with shared error handling  
65+     // Write  initial content to temp file  
6566    const  write_data : []const  u8  =  if  (cmd .data ) | d |  d  else  "" ;
6667    file_manager .writeTempFile (temp_path , write_data ) catch  | err |  switch  (err ) {
6768        error .PathAlreadyExists  = >  {
@@ -76,26 +77,26 @@ pub fn handleOpenCommand(allocator: std.mem.Allocator, writer: std.io.AnyWriter,
7677        else  = >  return  err ,
7778    };
7879
79-     var  watcher_ptr : ? * FileWatcher  =  null ;
80-     var  watcher_ctx_ptr : ? * FileWatcherContext  =  null ;
80+     var  watcher : ? * FileWatcher  =  null ;
81+     var  watcher_ctx : ? * FileWatcherContext  =  null ;
8182
8283    // Start file watcher if data_on_save is true 
8384    if  (cmd .data_on_save ) {
84-         const   watcher_ctx  =  try  allocator .create (FileWatcherContext );
85-         watcher_ctx .*   =  .{ . allocator   =   allocator , . writer   =   writer , . base_dir   =   config . base_dir , . token   =   cmd . token , . temp_path   =   temp_path  } ;
86- 
87-          const   watcher   =   try   allocator . create ( FileWatcher ); 
88-         watcher .*  =  try  FileWatcher . init ( allocator ,  temp_path ,  fileChangedCallback ,  watcher_ctx );
89-         try   watcher . start ( );
90- 
91-         watcher_ptr   =   watcher ;
92-         watcher_ctx_ptr   =   watcher_ctx ;
85+         watcher_ctx  =  try  allocator .create (FileWatcherContext );
86+         errdefer   allocator . destroy ( watcher_ctx .? ) ;
87+          watcher_ctx .?.*   =  .{ . allocator   =   allocator , . writer   =   writer , . base_dir   =   config . base_dir , . token   =   cmd . token , . temp_path   =   temp_path  }; 
88+ 
89+         watcher  =  try  allocator . create ( FileWatcher );
90+         errdefer   allocator . destroy ( watcher .? );
91+          watcher .?.*   =   try   FileWatcher . init ( allocator ,  temp_path ,  fileChangedCallback ,  watcher_ctx .? ); 
92+         try   watcher .? . start () ;
93+         errdefer   watcher .? . deinit () ;
9394    }
9495
9596    // Spawn editor in a separate thread and track lifecycle with wait group 
9697    const  editor_cmd  =  config .getEditor (hostname , remote_path );
9798    wait_group .start ();
98-     const  thread  =  Thread .spawn (.{}, editorThread , .{ allocator , writer , config .base_dir , editor_cmd , temp_path , cmd .token , watcher_ptr ,  watcher_ctx_ptr , wait_group  }) catch  | err |  {
99+     const  thread  =  Thread .spawn (.{}, editorThread , .{ allocator , writer , config .base_dir , editor_cmd , temp_path , cmd .token , watcher ,  watcher_ctx , wait_group  }) catch  | err |  {
99100        wait_group .finish ();
100101        return  err ;
101102    };
@@ -152,6 +153,7 @@ fn editorThread(allocator: std.mem.Allocator, writer: std.io.AnyWriter, base_dir
152153
153154    // Cleanup temp file and any empty parent directories 
154155    file_manager .cleanupTempPath (base_dir , temp_path );
156+     allocator .free (temp_path );
155157
156158    log .info ("Editor closed for file: {s}" , .{token });
157159    // Signal completion to wait group last 
0 commit comments