diff --git a/Runtime/Scripts/CameraVideoSource.cs b/Runtime/Scripts/CameraVideoSource.cs index 05dad0a8..354ee251 100644 --- a/Runtime/Scripts/CameraVideoSource.cs +++ b/Runtime/Scripts/CameraVideoSource.cs @@ -51,6 +51,17 @@ public override void Stop() ClearRenderTexture(); } + protected override void Dispose(bool disposing) + { + if (disposing && _renderTexture != null) + { + _renderTexture.Release(); + UnityEngine.Object.Destroy(_renderTexture); + _renderTexture = null; + } + base.Dispose(disposing); + } + private void ClearRenderTexture() { if (_renderTexture) @@ -71,6 +82,18 @@ protected override bool ReadBuffer() { if (_renderTexture == null || _renderTexture.width != GetWidth() || _renderTexture.height != GetHeight()) { + // Free previously allocated GPU/native resources before reallocating; + // otherwise the old textures and NativeArray leak on every resolution change. + if (_renderTexture != null) + { + _renderTexture.Release(); + UnityEngine.Object.Destroy(_renderTexture); + } + if (_previewTexture != null) + UnityEngine.Object.Destroy(_previewTexture); + if (_captureBuffer.IsCreated) + _captureBuffer.Dispose(); + var targetFormat = Utils.GetSupportedGraphicsFormat(SystemInfo.graphicsDeviceType); var compatibleFormat = SystemInfo.GetCompatibleFormat(targetFormat, FormatUsage.ReadPixels); _textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat); diff --git a/Runtime/Scripts/ScreenVideoSource.cs b/Runtime/Scripts/ScreenVideoSource.cs index 2e3321e0..2dbe2b1e 100644 --- a/Runtime/Scripts/ScreenVideoSource.cs +++ b/Runtime/Scripts/ScreenVideoSource.cs @@ -45,6 +45,17 @@ public override void Stop() ClearRenderTexture(); } + protected override void Dispose(bool disposing) + { + if (disposing && _renderTexture != null) + { + _renderTexture.Release(); + UnityEngine.Object.Destroy(_renderTexture); + _renderTexture = null; + } + base.Dispose(disposing); + } + private void ClearRenderTexture() { if (_renderTexture) @@ -65,6 +76,18 @@ protected override bool ReadBuffer() { if (_renderTexture == null || _renderTexture.width != GetWidth() || _renderTexture.height != GetHeight()) { + // Free previously allocated GPU/native resources before reallocating; + // otherwise the old textures and NativeArray leak on every resolution change. + if (_renderTexture != null) + { + _renderTexture.Release(); + UnityEngine.Object.Destroy(_renderTexture); + } + if (_previewTexture != null) + UnityEngine.Object.Destroy(_previewTexture); + if (_captureBuffer.IsCreated) + _captureBuffer.Dispose(); + var targetFormat = Utils.GetSupportedGraphicsFormat(SystemInfo.graphicsDeviceType); var compatibleFormat = SystemInfo.GetCompatibleFormat(targetFormat, FormatUsage.ReadPixels); _textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat); diff --git a/Runtime/Scripts/WebCameraSource.cs b/Runtime/Scripts/WebCameraSource.cs index b550f632..7a093b2f 100644 --- a/Runtime/Scripts/WebCameraSource.cs +++ b/Runtime/Scripts/WebCameraSource.cs @@ -47,6 +47,17 @@ public WebCameraSource(WebCamTexture texture, VideoBufferType bufferType = Video Dispose(false); } + protected override void Dispose(bool disposing) + { + if (_tempTexture != null) + { + _tempTexture.Release(); + UnityEngine.Object.Destroy(_tempTexture); + _tempTexture = null; + } + base.Dispose(disposing); + } + private Color32[] _readBuffer; // Read the texture data into a native array asynchronously @@ -64,7 +75,16 @@ protected override bool ReadBuffer() _previewTexture.width != width || _previewTexture.height != height) { - // Required when using Allocator.Persistent + // Free previously allocated GPU/native resources before reallocating; + // otherwise the old textures and NativeArray leak on every resolution change. + if (_previewTexture != null) + UnityEngine.Object.Destroy(_previewTexture); + if (_tempTexture != null) + { + _tempTexture.Release(); + UnityEngine.Object.Destroy(_tempTexture); + _tempTexture = null; + } if (_captureBuffer.IsCreated) _captureBuffer.Dispose(); diff --git a/Samples~/Meet/Assets/Runtime/MeetManager.cs b/Samples~/Meet/Assets/Runtime/MeetManager.cs index 8ad1bfee..fed49205 100644 --- a/Samples~/Meet/Assets/Runtime/MeetManager.cs +++ b/Samples~/Meet/Assets/Runtime/MeetManager.cs @@ -81,6 +81,14 @@ private void Update() private void OnDestroy() { + // Without this, scene change / app quit while connected leaves all tracks, + // streams, and their backing GPU/native resources allocated. + if (_room != null) + { + _room.Disconnect(); + _room = null; + } + CleanUpAllTracks(); _webCamTexture?.Stop(); }