Ensure HeapBytesStore has final fields for correct visibility, https://github.com/OpenHFT/Chronicle-Wire/issues/137

This commit is contained in:
Peter Lawrey
2019-05-08 19:27:15 +01:00
parent 0b09733cc9
commit 6711903d2c
4 changed files with 21 additions and 18 deletions
@@ -43,7 +43,7 @@ public class ByteArrayDataAccess extends AbstractData<byte[]> implements DataAcc
}
private void initTransients() {
bs = HeapBytesStore.uninitialized();
bs = null;
}
@Override
@@ -77,14 +77,14 @@ public class ByteArrayDataAccess extends AbstractData<byte[]> implements DataAcc
@Override
public Data<byte[]> getData(@NotNull byte[] instance) {
array = instance;
bs.init(instance);
bs = HeapBytesStore.wrap(array);
return this;
}
@Override
public void uninit() {
array = null;
bs.uninit();
bs = null;
}
@Override
@@ -32,7 +32,6 @@ public class ByteBufferDataAccess extends AbstractData<ByteBuffer>
implements DataAccess<ByteBuffer> {
// Cache fields
private transient HeapBytesStore heapBytesStore;
private transient NativeBytesStore nativeBytesStore;
private transient VanillaBytes<Void> bytes;
@@ -45,7 +44,6 @@ public class ByteBufferDataAccess extends AbstractData<ByteBuffer>
}
private void initTransients() {
heapBytesStore = HeapBytesStore.uninitialized();
nativeBytesStore = NativeBytesStore.uninitialized();
bytes = VanillaBytes.vanillaBytes();
}
@@ -91,8 +89,7 @@ public class ByteBufferDataAccess extends AbstractData<ByteBuffer>
nativeBytesStore.init(instance, false);
bytesStore = nativeBytesStore;
} else {
heapBytesStore.init(instance);
bytesStore = heapBytesStore;
bytesStore = HeapBytesStore.wrap(instance);
}
return this;
}
@@ -102,8 +99,6 @@ public class ByteBufferDataAccess extends AbstractData<ByteBuffer>
bb = null;
if (bytesStore == nativeBytesStore) {
nativeBytesStore.uninit();
} else {
heapBytesStore.uninit();
}
}
@@ -29,7 +29,12 @@ public enum ZeroBytesStore implements BytesStore<ZeroBytesStore, Void> {
@Override
public long addressForWrite(long offset) throws UnsupportedOperationException, BufferOverflowException {
throw new UnsupportedOperationException("todo");
throw new UnsupportedOperationException("Cannot get the address of nothing");
}
@Override
public long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException {
throw new UnsupportedOperationException("Cannot get the address of nothing");
}
@Override
+11 -8
View File
@@ -41,6 +41,7 @@ import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
@Ignore("Long running")
public class MemoryLeaksTest {
/**
@@ -61,7 +62,7 @@ public class MemoryLeaksTest {
this.closeWithinContext = closeWithinContext;
builder = ChronicleMap
.of(IntValue.class, String.class).constantKeySizeBySample(Values.newHeapInstance(IntValue.class))
.valueReaderAndDataAccess(new CountedStringReader(), new StringUtf8DataAccess());
.valueReaderAndDataAccess(new CountedStringReader(this), new StringUtf8DataAccess());
if (replicated)
builder.replication((byte) 1);
builder.entries(1).averageValueSize(10);
@@ -220,15 +221,16 @@ public class MemoryLeaksTest {
}
}
private class CountedStringReader extends StringSizedReader {
private static class CountedStringReader extends StringSizedReader {
private transient MemoryLeaksTest memoryLeaksTest;
private final String creationStackTrace;
private final Cleaner cleaner;
CountedStringReader() {
serializerCount.incrementAndGet();
serializers.add(new WeakReference<>(this));
cleaner = CleanerUtils.createCleaner(this, serializerCount::decrementAndGet);
CountedStringReader(MemoryLeaksTest memoryLeaksTest) {
this.memoryLeaksTest = memoryLeaksTest;
this.memoryLeaksTest.serializerCount.incrementAndGet();
this.memoryLeaksTest.serializers.add(new WeakReference<>(this));
cleaner = CleanerUtils.createCleaner(this, this.memoryLeaksTest.serializerCount::decrementAndGet);
try (StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter)) {
new Exception().printStackTrace(printWriter);
@@ -237,11 +239,12 @@ public class MemoryLeaksTest {
} catch (IOException e) {
throw new RuntimeException(e);
}
this.memoryLeaksTest = memoryLeaksTest;
}
@Override
public CountedStringReader copy() {
return new CountedStringReader();
return new CountedStringReader(this.memoryLeaksTest);
}
}
}