C# 数据完整性问题
我的问题是下面的decodedProxyExcerpt2的分配覆盖了decodedProxyExcerpt1,我不知道为什么。
有什么线索吗?
提前致谢。
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);
}}
my problem is that the assignment of decodedProxyExcerpt2 below overwrites decodedProxyExcerpt1 and I do not know why.
Any clues?
Thanks in advance.
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];
}
}
From AudioFactory:
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);
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我向一位朋友询问了这个问题,他给了我提示,我可能一直在考虑在 C++ 中,数组的赋值创建一个副本,而不是 C# 中,数组的赋值创建一个引用。
如果这是正确的并且
已解码ProxyExcerpt1 = audiofactory.Current(stepSize);
如果设置一个引用(而不是副本),那么覆盖是完全可以理解的。
I asked a friend about this who gave me the hint that I might have been thinking in C++ where assignment of an array creates a copy, instead of C# where assignment of an array creates a reference.
If that is correct and
decodedProxyExcerpt1 = audiofactory.Current(stepSize);
is setting a reference (not a copy) then the overwrite is completely understandable.
从表面上看,
audiofactory.MoveNext(stepSize)
保持在相同的参考位置。 这导致audiofactory.Current(stepSize)
停留在同一地址。因此,
decodedProxyExcerpt1
和decodedProxyExcerpt2
指向相同的引用,因此对一个引用的更改会传播到另一个。因此,问题出在您的
AudioFactory
类中。From the looks of it
audiofactory.MoveNext(stepSize)
is staying at the same reference. This is causingaudiofactory.Current(stepSize)
to stay at the same address.For this reason, but
decodedProxyExcerpt1
anddecodedProxyExcerpt2
point to the same reference and therefore changes to one propagate to the other.So, the problems lies in your
AudioFactory
class.类的实例作为引用存储。
解码代理Excerpt1和解码代理Excerpt2都是对同一对象的引用——audiofactory.CurrentExcerpt。
Instances of classes are stored as references.
decodedProxyExcerpt1 and decodedProxyExcerpt2 are both references to the same object -- audiofactory.CurrentExcerpt.