瀏覽代碼

file watchers: fix leak

lviguier 1 年之前
父節點
當前提交
42c8bce6bc
共有 2 個文件被更改,包括 31 次插入5 次删除
  1. 0 1
      hxd/fs/FileEntry.hx
  2. 31 4
      hxd/fs/LocalFileSystem.hx

+ 0 - 1
hxd/fs/FileEntry.hx

@@ -9,7 +9,6 @@ class FileEntry {
 	public var size(get, never) : Int;
 	public var size(get, never) : Int;
 	public var isDirectory(get, never) : Bool;
 	public var isDirectory(get, never) : Bool;
 	public var isAvailable(get, never) : Bool;
 	public var isAvailable(get, never) : Bool;
-	var watchOnChangedHistory : Array<Null<Void -> Void>>;
 
 
 	public function getBytes() : haxe.io.Bytes return null;
 	public function getBytes() : haxe.io.Bytes return null;
 	public function readBytes( out : haxe.io.Bytes, outPos : Int, pos : Int, len : Int ) : Int { throw "readBytes() not implemented"; }
 	public function readBytes( out : haxe.io.Bytes, outPos : Int, pos : Int, len : Int ) : Int { throw "readBytes() not implemented"; }

+ 31 - 4
hxd/fs/LocalFileSystem.hx

@@ -99,6 +99,17 @@ class LocalEntry extends FileEntry {
 	}
 	}
 
 
 	var watchCallback : Void -> Void;
 	var watchCallback : Void -> Void;
+
+	/*
+	When a resource is load, we add a watcher on it and wtih callback to call
+	when this resource is modified. The problem is that in editor, several engine works
+	in parallel, and if the same resource is load by different engine, we have
+	to reload this resource for each engine when the file is modified (resulting
+	in one file watcher with multiple callback). This problem occures only in editor (because
+	games contains only one engine) so this feature is editor only.
+	*/
+	#if editor var watchOnChangedHistory : Array<Null<Void -> Void>>; #end
+
 	#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
 	#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
 	var watchHandle : hl.uv.Fs;
 	var watchHandle : hl.uv.Fs;
 	var lastChanged : Float = 0;
 	var lastChanged : Float = 0;
@@ -168,11 +179,13 @@ class LocalEntry extends FileEntry {
 			if( watchCallback != null ) {
 			if( watchCallback != null ) {
 				WATCH_LIST.remove(this);
 				WATCH_LIST.remove(this);
 				watchCallback = null;
 				watchCallback = null;
+				#if editor watchOnChangedHistory = null; #end
 				#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
 				#if (hl && (hl_ver >= version("1.12.0")) && !usesys)
 				watchHandle.close();
 				watchHandle.close();
 				watchHandle = null;
 				watchHandle = null;
 				#end
 				#end
 			}
 			}
+
 			return;
 			return;
 		}
 		}
 		if( watchCallback == null ) {
 		if( watchCallback == null ) {
@@ -193,6 +206,11 @@ class LocalEntry extends FileEntry {
 					#end
 					#end
 					w.watchCallback = null;
 					w.watchCallback = null;
 					WATCH_LIST.remove(w);
 					WATCH_LIST.remove(w);
+
+					#if editor
+					if (w.watchOnChangedHistory != null)
+						this.watchOnChangedHistory = w.watchOnChangedHistory.copy();
+					#end
 				}
 				}
 			WATCH_LIST.push(this);
 			WATCH_LIST.push(this);
 		}
 		}
@@ -218,22 +236,31 @@ class LocalEntry extends FileEntry {
 		watchTime = getModifTime();
 		watchTime = getModifTime();
 		#end
 		#end
 
 
+		#if editor
 		if (watchOnChangedHistory == null)
 		if (watchOnChangedHistory == null)
-			watchOnChangedHistory = new Array<Null<Void -> Void>>();
+			watchOnChangedHistory = [ onChanged ];
+		else
+			watchOnChangedHistory.push(onChanged);
+		#end
 
 
-		watchOnChangedHistory.push(onChanged);
 		watchCallback = function() {
 		watchCallback = function() {
 			fs.convert.run(this);
 			fs.convert.run(this);
 
 
-			var idx = watchOnChangedHistory.length -1;
+			#if editor
+			if (watchOnChangedHistory == null)
+				return;
+
+			var idx = watchOnChangedHistory.length - 1;
 			while (idx >= 0) {
 			while (idx >= 0) {
 				if (watchOnChangedHistory[idx] != null)
 				if (watchOnChangedHistory[idx] != null)
 					watchOnChangedHistory[idx]();
 					watchOnChangedHistory[idx]();
 				idx--;
 				idx--;
 			}
 			}
+			#else
+			onChanged();
+			#end
 		}
 		}
 	}
 	}
-
 }
 }
 
 
 class LocalFileSystem implements FileSystem {
 class LocalFileSystem implements FileSystem {