2009-04-21 3 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]; 
    } 

} 

:

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.MoveNext(stepSize)이 같은 기준에 머물고 그것의 모습에서. 이로 인해 audiofactory.Current(stepSize)은 같은 주소에 머물러 있습니다.

이러한 이유로 인해 decodedProxyExcerpt1decodedProxyExcerpt2은 동일한 참조를 가리키고 하나는 다른 참조로 변경됩니다.

따라서 문제는 AudioFactory 클래스에 있습니다.

0

친구에게이 질문에 대해 C + +에서 생각해봤을지도 모르는 힌트를주었습니다. 배열을 할당하면 참조가 만들어지는 C# 대신 복사본이 만들어집니다.

이 올바른 경우와

decodedProxyExcerpt1 = audiofactory.Current (스텝 크기);

은 참조 (사본이 아님)를 설정하면 덮어 쓰기를 완전히 이해할 수 있습니다.