2009-04-21 20 views
1

私の問題は、下のdecodedProxyExcerpt2の割り当てが、decodedProxyExcerpt1を上書きし、なぜそれがわからないということです。データの完全性の問題C#

手がかりはありますか?

ありがとうございます。 AudioFactoryから

 DecodedProxyExcerpt decodedProxyExcerpt1 = new DecodedProxyExcerpt(stepSize); 
     if (audiofactory.MoveNext(stepSize)) 
     { 
      decodedProxyExcerpt1 = audiofactory.Current(stepSize); 
     } 
     // At this point decodedProxyExcerpt1.data contains the correct values. 

     DecodedProxyExcerpt decodedProxyExcerpt2 = new DecodedProxyExcerpt(stepSize); 
     if (audiofactory.MoveNext(stepSize)) 
     { 
      decodedProxyExcerpt2 = audiofactory.Current(stepSize); 
     } 
     // At this point decodedProxyExcerpt2.data contains the correct values. 
     // However, decodedProxyExcerpt1.data is overwritten and now holds the values of decodedProxyExcerpt2.data. 


public class DecodedProxyExcerpt 
{ 
    public short[] data { get; set; } // PCM data 

    public DecodedProxyExcerpt(int size) 
    { 
     this.data = new short[size]; 
    } 

} 

:それaudiofactory.MoveNext(stepSize)のルックスから

public bool MoveNext(int stepSize) 
    { 
     if (index == -1) 
     { 
      index = 0; 
      return (true); 
     } 
     else 
     { 
      index = index + stepSize; 
      if (index >= buffer.Length - stepSize) 
       return (false); 
      else 
       return (true); 
     } 
    } 

    public DecodedProxyExcerpt Current(int stepSize) 
    { 
     Array.Copy(buffer, index, CurrentExcerpt.data, 0, stepSize); 
     return(CurrentExcerpt); 
    }} 
+0

実際に何が起こっているかを知るのに十分な情報はありません。問題はaudioFactory内にある可能性があります。 – cjk

+1

audiofactory.Current(stepSize)のコードを投稿してください。明らかにエラーがあります。 –

+0

+1のバグはどこにあるのですか? 7年後にC#や.NETでデータの完全性の問題を想像するのはかなり難しいです。 CLRの内部やマルチプロセッシングコードの中で深いものですが、シンプルなシングルスレッドコードではありません。 –

答えて

1

クラスのインスタンスは参照として格納されます。

decodedProxyExcerpt1とdecodedProxyExcerpt2は、両方とも同じオブジェクト(audiofactory.CurrentExcerpt)への参照です。

4

には同一の参照に滞在しています。これにより、audiofactory.Current(stepSize)は同じアドレスに留まります。

このため、decodedProxyExcerpt1decodedProxyExcerpt2は同じ参照を指しているため、一方が他方に伝搬します。

したがって、問題はAudioFactoryクラスにあります。

0

私は、配列の割り当てによって参照が作成されるC#の代わりに、配列の割り当てがコピーを作成するC++で考えていたかもしれないヒントを私に教えてくれました。

それが正しい場合と

decodedProxyExcerpt1 = audiofactory.Current(ステップサイズ)。

は参照(コピーではない)を設定しているため、上書きは完全に理解できます。

関連する問題