Skip to content

Commit 3eebaa4

Browse files
committed
box - finally free resources
1 parent 4ecf1e0 commit 3eebaa4

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

lib/src/box.dart

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ class Box<T> {
5252

5353
// put object into box and free the buffer
5454
ByteBuffer buffer = _fbManager.marshal(propVals);
55-
checkObx(bindings.obx_box_put(
56-
_cBox, propVals[_modelEntity.idPropName], buffer.voidPtr, buffer.size, _getOBXPutMode(mode)));
57-
buffer.free();
55+
try {
56+
checkObx(bindings.obx_box_put(
57+
_cBox, propVals[_modelEntity.idPropName], buffer.voidPtr, buffer.size, _getOBXPutMode(mode)));
58+
} finally {
59+
buffer.free();
60+
}
5861
return propVals[_modelEntity.idPropName];
5962
}
6063

@@ -91,15 +94,20 @@ class Box<T> {
9194
// because obx_box_put_many also needs a list of all IDs of the elements to be put into the box,
9295
// generate this list now (only needed if not all IDs have been generated)
9396
Pointer<Uint64> allIdsMemory = Pointer<Uint64>.allocate(count: objects.length);
94-
for (int i = 0; i < allPropVals.length; ++i)
95-
allIdsMemory.elementAt(i).store(allPropVals[i][_modelEntity.idPropName] as int);
96-
97-
// marshal all objects to be put into the box
98-
var putObjects = ByteBufferArray(allPropVals.map<ByteBuffer>(_fbManager.marshal).toList()).toOBXBytesArray();
97+
try {
98+
for (int i = 0; i < allPropVals.length; ++i)
99+
allIdsMemory.elementAt(i).store(allPropVals[i][_modelEntity.idPropName] as int);
99100

100-
checkObx(bindings.obx_box_put_many(_cBox, putObjects.ptr, allIdsMemory, _getOBXPutMode(mode)));
101-
putObjects.free();
102-
allIdsMemory.free();
101+
// marshal all objects to be put into the box
102+
var putObjects = ByteBufferArray(allPropVals.map<ByteBuffer>(_fbManager.marshal).toList()).toOBXBytesArray();
103+
try {
104+
checkObx(bindings.obx_box_put_many(_cBox, putObjects.ptr, allIdsMemory, _getOBXPutMode(mode)));
105+
} finally {
106+
putObjects.free();
107+
}
108+
} finally {
109+
allIdsMemory.free();
110+
}
103111
return allPropVals.map((p) => p[_modelEntity.idPropName] as int).toList();
104112
}
105113

@@ -109,15 +117,19 @@ class Box<T> {
109117

110118
// get element with specified id from database
111119
return _store.runInTransaction(TxMode.Read, () {
112-
checkObx(bindings.obx_box_get(_cBox, id, dataPtr, sizePtr));
120+
ByteBuffer buffer;
121+
try {
122+
checkObx(bindings.obx_box_get(_cBox, id, dataPtr, sizePtr));
113123

114-
Pointer<Uint8> data = Pointer<Uint8>.fromAddress(dataPtr.load<Pointer<Void>>().address);
115-
var size = sizePtr.load<int>();
124+
Pointer<Uint8> data = Pointer<Uint8>.fromAddress(dataPtr.load<Pointer<Void>>().address);
125+
var size = sizePtr.load<int>();
116126

117-
// transform bytes from memory to Dart byte list
118-
var buffer = ByteBuffer(data, size);
119-
dataPtr.free();
120-
sizePtr.free();
127+
// transform bytes from memory to Dart byte list
128+
buffer = ByteBuffer(data, size);
129+
} finally {
130+
dataPtr.free();
131+
sizePtr.free();
132+
}
121133

122134
return _fbManager.unmarshal(buffer);
123135
});

0 commit comments

Comments
 (0)