From d2af52784f1ba08d4b1c41d3aa3f7352545c3ae8 Mon Sep 17 00:00:00 2001 From: yyagi Date: Fri, 16 Mar 2018 00:51:01 +0900 Subject: [PATCH] =?utf8?q?#38036=20CTexture()=E3=81=AB=E3=81=A4=E3=81=84?= =?utf8?q?=E3=81=A6=E3=80=81=E3=83=95=E3=82=A1=E3=82=A4=E3=83=8A=E3=83=A9?= =?utf8?q?=E3=82=A4=E3=82=B6=E3=81=A7=E3=82=A2=E3=83=B3=E3=83=9E=E3=83=8D?= =?utf8?q?=E3=83=BC=E3=82=B8=E3=83=89=E3=83=AA=E3=82=BD=E3=83=BC=E3=82=B9?= =?utf8?q?=E3=82=92Dispose=E3=81=97=E3=81=AA=E3=81=84=E3=82=88=E3=81=86?= =?utf8?q?=E4=BF=AE=E6=AD=A3=E3=80=82=E3=81=BE=E3=81=9F=E3=80=81=E3=83=95?= =?utf8?q?=E3=82=A1=E3=82=A4=E3=83=8A=E3=83=A9=E3=82=A4=E3=82=BA=E6=99=82?= =?utf8?q?=E3=81=ABSharpDX=E3=81=AETexture=E3=83=AA=E3=82=BD=E3=83=BC?= =?utf8?q?=E3=82=B9=E3=81=8C=E6=AE=8B=E3=81=A3=E3=81=A6=E3=81=84=E3=82=8B?= =?utf8?q?=E5=A0=B4=E5=90=88=E3=81=AF=E3=83=AD=E3=82=B0=E3=81=AB=E8=AD=A6?= =?utf8?q?=E5=91=8A=E3=82=92=E5=87=BA=E3=81=99=E3=82=88=E3=81=86=E3=81=AB?= =?utf8?q?=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- FDK/コード/04.グラフィック/CTexture.cs | 39 ++++++++++++++++++++----- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/FDK/コード/04.グラフィック/CTexture.cs b/FDK/コード/04.グラフィック/CTexture.cs index f88da46d..bf05520d 100644 --- a/FDK/コード/04.グラフィック/CTexture.cs +++ b/FDK/コード/04.グラフィック/CTexture.cs @@ -12,6 +12,16 @@ using Rectangle = System.Drawing.Rectangle; namespace FDK { + /// + /// テクスチャを扱うクラス。 + /// 使用終了時は必ずDispose()してください。Finalize時の自動Disposeはありません。 + /// Disposeを忘れた場合は、メモリリークに直結します。 + /// Finalize時にDisposeしない代わりに、Finalize時にテクスチャのDispose漏れを検出し、 + /// Trace.TraceWarning()でログを出力します。 + /// see also: + /// https://osdn.net/projects/dtxmania/ticket/38036 + /// https://github.com/sharpdx/SharpDX/pull/192?w=1 + /// public class CTexture : IDisposable { #region [ プロパティ ] @@ -84,6 +94,7 @@ namespace FDK this.szテクスチャサイズ = new Size( 0, 0 ); this._透明度 = 0xff; this.texture = null; + this.bSharpDXTextureDispose完了済み = true; this.cvPositionColoredVertexies = null; this.b加算合成 = false; this.fZ軸中心回転 = 0f; @@ -91,7 +102,7 @@ namespace FDK this.bFlipY = false; // this._txData = null; } - + /// /// 指定されたビットマップオブジェクトから Managed テクスチャを作成する。 /// テクスチャのサイズは、BITMAP画像のサイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。 @@ -119,6 +130,7 @@ namespace FDK stream.Seek( 0L, SeekOrigin.Begin ); int colorKey = unchecked( (int) 0xFF000000 ); this.texture = Texture.FromStream( device, stream, this.szテクスチャサイズ.Width, this.szテクスチャサイズ.Height, 1, Usage.None, format, poolvar, Filter.Point, Filter.None, colorKey ); + this.bSharpDXTextureDispose完了済み = false; } } catch ( Exception e ) @@ -213,6 +225,7 @@ namespace FDK #endif // 中で更にメモリ読み込みし直していて無駄なので、Streamを使うのは止めたいところ this.texture = Texture.FromStream( device, stream, n幅, n高さ, 1, usage, format, pool, Filter.Point, Filter.None, colorKey ); + this.bSharpDXTextureDispose完了済み = false; } } } @@ -276,6 +289,7 @@ namespace FDK // { //Trace.TraceInformation( "CTexture() start: " ); this.texture = Texture.FromMemory( device, txData, this.sz画像サイズ.Width, this.sz画像サイズ.Height, 1, Usage.None, format, pool, Filter.Point, Filter.None, colorKey ); + this.bSharpDXTextureDispose完了済み = false; //Trace.TraceInformation( "CTexture() end: " ); // } } @@ -353,6 +367,7 @@ namespace FDK #endif texture.UnlockRectangle( 0 ); bitmap.UnlockBits( srcBufData ); + this.bSharpDXTextureDispose完了済み = false; } //Trace.TraceInformation( "CTExture() End: " ); } @@ -603,20 +618,28 @@ namespace FDK if (disposeManagedObjects) { // (A) Managed リソースの解放 + // テクスチャの破棄 (SharpDXのテクスチャは、SharpDX側で管理されるため、FDKからはmanagedリソースと見做す) + if (this.texture != null) + { + this.texture.Dispose(); + this.texture = null; + this.bSharpDXTextureDispose完了済み = true; + } } // (B) Unamanaged リソースの解放 - // テクスチャの破棄 (テクスチャはサイズが大きい=LOHに格納される=GCで回収されない=unmanaged扱いする(以前からそうしていましたが改めて)) - if (this.texture != null) - { - this.texture.Dispose(); - this.texture = null; - } + this.bDispose完了済み = true; } ~CTexture() { + // ファイナライザの動作時にtextureのDisposeがされていない場合は、 + // CTextureのDispose漏れと見做して警告をログ出力する + if (!this.bSharpDXTextureDispose完了済み) + { + Trace.TraceWarning("CTexture: Dispose漏れを検出しました。(Size=({0}, {1}))", sz画像サイズ.Width, sz画像サイズ.Height ); + } this.Dispose(false); } //----------------- @@ -628,7 +651,7 @@ namespace FDK #region [ private ] //----------------- protected int _透明度; - private bool bDispose完了済み; + private bool bDispose完了済み, bSharpDXTextureDispose完了済み; protected PositionColoredTexturedVertex[] cvPositionColoredVertexies; protected TransformedColoredTexturedVertex[] cvTransformedColoredVertexies; protected const Pool poolvar = // 2011.4.25 yyagi -- 2.11.0