gpt4 book ai didi

java - 最终改变的属性

转载 作者:行者123 更新时间:2023-11-29 09:16:32 25 4
gpt4 key购买 nike

没有明显的原因或解决方案,我们遇到了迄今为​​止无法解决的错误。

在多次访问包含从文件加载的数据的阵列时,会发生此错误。
ArrayIndexOutOfBounds出现在与此数组一起工作的代码的各个部分中。

这些变量似乎会随机更改值。为了避免这种情况,我们尝试在所有的类属性中添加修饰符“ final”。但是它们仍然会随机更改值。

在下面,方法“ execute()”及其内部循环“ while”是发生错误的应用程序点。

public Set<Long> execute(InputStream datIn, int qtd) throws PersistenceException {

if (this.criterion == null) {
throw new PersistenceException("Não foi especificado filtro para a busca");
}

this.criterion.configure(info);

try {
Set<Long> retorno = new HashSet<Long>(qtd);
byte[] b = new byte[Serializator.BUFFER_SIZE];
int bl = datIn.read(b, 0, Serializator.BUFFER_SIZE);
int br = bl, bp = 0, nbp = 0, blockSize, i, p = 0;

while (p++ < qtd) {
System.out.println("P = "+p);
bp = nbp;
int aux = ByteArrayUtils.toUnsignedShort(b, bp);
System.out.println("aux = "+aux);
blockSize = aux + indexBlockSize;
System.out.println("indexBlockSize = "+indexBlockSize);
System.out.println("blockSize = "+blockSize);
if (br < blockSize + 2) {
for (i = 0; i < br; i++) {
b[i] = b[i + bp];
}
bl = datIn.read(b, br, Serializator.BUFFER_SIZE - br) + br;
bp = 0;
br = bl;
}

nbp = bp + blockSize;
br -= blockSize;

System.out.println("b.length = "+b.length);
System.out.println("bp = "+bp);
System.out.println("headerSize = "+headerSize);
if (this.criterion.doCompare(b, bp, headerSize)) {
retorno.add(getSearchedValue(b, bp));
if (retorno.size() == this.maxResults) {
break;
}
}
}

return retorno;
} catch (IOException e) {
throw new PersistenceException(e);
}
}


下面的代码出现在“ execute()”中:

getSearchValue:

public long getSearchedValue(byte[] b, int bp) {
if (this.fieldReturn == -1) {
return ByteArrayUtils.toLong(b, bp + 2);
} else {
if (b[this.bitSetPosition + bp] >= 0) {
int off = ByteArrayUtils.toShort(b, bp + rIndex) + headerSize + bp;
return ByteArrayUtils.readOptimizedLong(b, off);
} else {
return 0;
}
}
}


ByteArraysUtils.toShort():

public static short toShort(byte[] byteArray, int off) {
System.out.println("ByteArrayUtils: toShort: byteArray.length="+byteArray.length
+" off = "+off);
return (short) ((byteArray[off + 1] & 0xFF) | ((byteArray[off] & 0xFF) << 8));
}


ByteArraysUtils.toUnsignedShort():

public static int toUnsignedShort(byte[] byteArray, int off) {
System.out.println("ByteArrayUtils: toUnsignedShort: byteArray.length="+byteArray.length
+" off = "+off);
return ((int) 0) | ((byteArray[off + 1] & 0xFF) | ((byteArray[off] & 0xFF) << 8));
}


Criterion.doCompare():

public boolean doCompare(byte[] data, int offset, int headerSize) throws PersistenceException {
System.out.println("doCompare: data.length = "+data.length+" offset = "+offset
+" this.position = "+this.position);
if (data[offset + this.position] >= 0) {
short off = ByteArrayUtils.toShort(data, offset + this.position);
return this.def.verify(data, off + offset + headerSize);
} else {
return this.def.verifyNull();
}
}


以下是执行期间生成的一些错误日志:


错误1:
造成原因:java.lang.ArrayIndexOutOfBoundsException
在com.wealthsystems.util.api.array.ByteArrayUtils.toShort(ByteArrayUtils.java:123)
在com.wealthsystems.persistence.impl.search.criterion.Criterion.doCompare(Criterion.java:64)
在com.wealthsystems.persistence.impl.search.DatSearch.execute(DatSearch.java:133)
错误2:
6841 LoginActivity错误登录java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
在com.wealthsystems.persistence.impl.search.criterion.Criterion.doCompare(Criterion.java:61)
错误3:
1002575 LoginActivity错误登录java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
在com.wealthsystems.util.api.array.ByteArrayUtils.toUnsignedShort(ByteArrayUtils.java:129)


如我们所见,错误发生在许多不同的位置,但是变量的值始终相同,即,错误似乎是随机的。这已经通过变量的打印得到了验证(因为当我们打开调试时,不会发生错误),并且通过打印,我们看到了奇怪的变量变化。以下是生成的一些打印示例:

打印1:

P = 746
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 22400
aux = 100
indexBlockSize = 58
blockSize = 158
b.length = 32767
bp = 22400
headerSize = 65
doCompare: data.length = 32767 offset = 22400 this.position = 10
ByteArrayUtils: toShort: byteArray.length=32767 off = 22410
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 22465

P = 747
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 22558
aux = 99
indexBlockSize = 58
blockSize = 157
b.length = 32767
bp = 22558
headerSize = 65
doCompare: data.length = 32767 offset = 22558 this.position = 10
java.lang.ArrayIndexOutOfBoundsException
at com.wealthsystems.persistence.impl.search.criterion.Criterion.doCompare(Criterion.java:62)
at com.wealthsystems.persistence.impl.search.DatSearch.execute(DatSearch.java:121)
at com.wealthsystems.persistence.impl.retrieve.DatRetrieve.search(DatRetrieve.java:62)


请注意,在“ doCompare()”中将访问索引“ [offset + this.position]”,但是在索引访问22568中会指出错误。索引小于向量的大小(“ data.length”) 。

列印2:

P = 578
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 28651
aux = 114
indexBlockSize = 58
blockSize = 172
b.length = 32767
bp = 28651
headerSize = 65
doCompare: data.length = 32767 offset = 28651 this.position = 10
ByteArrayUtils: toShort: byteArray.length=32767 off = 28661
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 28716

P = 579
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 28823
aux = 114
indexBlockSize = 58
blockSize = 22618
b.length = 32767
bp = 0
headerSize = 65
doCompare: data.length = 32767 offset = 0 this.position = 10
ByteArrayUtils: toShort: byteArray.length=32767 off = 10
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 65

P = 580
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 22618
aux = 65280
indexBlockSize = 58
blockSize = 87784
b.length = 32767
bp = 0
headerSize = 65
doCompare: data.length = 32767 offset = 0 this.position = 10
P = 581
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 87784
28493 LoginActivity Falha de login java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
at com.wealthsystems.util.api.array.ByteArrayUtils.toUnsignedShort(ByteArrayUtils.java:131)
at com.wealthsystems.persistence.impl.search.DatSearch.execute(DatSearch.java:98)
at com.wealthsystems.persistence.impl.retrieve.DatRetrieve.search(DatRetrieve.java:62)


注意执行p = 579。
aux值为114。indexBlockSize的值为58。blockSize的值为172。但是它出现的值为blockSize = 22618。

打印3:

P = 1
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 0
aux = 144
indexBlockSize = 58
blockSize = 202
b.length = 32767
bp = 0
headerSize = 65
doCompare: data.length = 32767 offset = 0 this.position = 10
ByteArrayUtils: toShort: byteArray.length=32767 off = 10
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 65

P = 2
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 202
aux = 96
indexBlockSize = 58
blockSize = 154
b.length = 32767
bp = 202
headerSize = 65
doCompare: data.length = 32767 offset = 202 this.position = 10
ByteArrayUtils: toShort: byteArray.length=32767 off = 212
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 267

P = 3
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 356
aux = 115
indexBlockSize = -7696
blockSize = -7581
b.length = 32767
bp = 356
headerSize = 65
doCompare: data.length = 32767 offset = 356 this.position = 10
ByteArrayUtils: toShort: byteArray.length=32767 off = 366
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = 421

P = 4
ByteArrayUtils: toUnsignedShort: byteArray.length=32767 off = -7225 61680
LoginActivity Falha de login java.lang.ArrayIndexOutOfBoundsException java.lang.ArrayIndexOutOfBoundsException
at com.wealthsystems.util.api.array.ByteArrayUtils.toUnsignedShort(ByteArrayUtils.java:131)
at com.wealthsystems.persistence.impl.search.DatSearch.execute(DatSearch.java:98)
at com.wealthsystems.persistence.impl.retrieve.DatRetrieve.search(DatRetrieve.java:62)


这是最严重的错误,因为变量“ indexBlockSize”是私有的,并在构造函数中初始化,并且不再修改,但是在运行过程中,它会更改indexBlockSize = -7696的值(执行P = 3)。

发生错误的设备:
 -iBAK-775(固件:2.1-update1-1.0.0)(内核:2.6.25)(频率:错误非常常见);
 -LG Optimus One P500(Android版本:2.2.2)(内核:2.6.32.9)(频率:错误不常见);
 -三星Galaxy Tab GT-P1000(Android:2.2)(频率:常见错误);
 -Samsung I5800 Galaxy 3(Android 2.2)(频率:常见错误)。

从未发生错误的设备:
 -在Eclipse中以调试模式运行时,上述所有设备;
 -在任何版本的Android上的模拟器;
 -Motorola Spice XT300(固件:2.1-update1)(内核:2.6.29);
 -Samsung Galaxy 5(GT-I5500B)(固件:2.1-update1)(内核:2.6.29);
 -摩托罗拉Xoom(Android 3.0);
 -三星Galaxy Tab 8.9(Android 3.0)。

不幸的是,我们无法在应用程序的外部代码中隔离该错误,从而使您能够重现该错误。将代码从我们的应用程序中隔离出来的所有尝试均不会产生该错误。但是希望您能给我们一些有关我们正在发生的事情的信息!

我们在互联网上针对此错误的研究仅显示以下页面: http://www.androiddiscuss.com/1-android-discuss/96129.html。看起来有人遇到了与我们类似的问题,但是讨论中没有解决方案。



属性的声明和类的构造函数:

private byte fieldReturn;
private EntityInfo info;
private final short rIndex, indexBlockSize;
private ICriterion criterion = null;
private int headerSize;
private int maxResults;
private int bitSetPosition;

public DatSearch(byte fieldReturn, EntityInfo info) {
this.fieldReturn = fieldReturn;
this.bitSetPosition = (this.fieldReturn == -1) ? -1 : 10 + (fieldReturn * 2);
this.info = info;
this.rIndex = (fieldReturn >= 0) ? (short) (10 + (fieldReturn << 1)) : 0;
short qtdIndices = (short) info.getMapper().getIndexes().length;
this.indexBlockSize = (short) (10 + (qtdIndices << 1));
this.headerSize = indexBlockSize + info.getBitSetLength();
}


我尝试将此类的所有变量放在外部的另一个类中,并使用gets和set方法读取它,但是此替代方法无效。

最佳答案

我假设您多次调用执行代码。在这种情况下,不同的线程可能正在访问您的阵列。当一个正在更改值时,另一个线程可能正在读取它们...

看一下java sync关键字。此关键字用于防止不同的线程访问相同的值。 more about synchronization

因此,在对阵列进行读/写操作时,请以同步方式包围这些调用,例如:

byte[] b;
synchronized(b)
{
byte[] b = new byte[Serializator.BUFFER_SIZE];
}

synchronized(b)
{
b[i] = b[i + bp];
}


您也可以执行此操作以获取完整的功能:

public synchronized long getSearchedValue(byte[] b, int bp) {
... your code
}


确保在所有更改崩溃的阵列的地方都这样做。

关于java - 最终改变的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9070141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com