@@ -35,7 +35,7 @@ class U8Array extends NativeArray<ffi.Uint8, int> {
3535 ptr[idx] = value;
3636 }
3737 }
38- finalizer.attach (this , ptr.cast ());
38+ finalizer.attach (this , ptr.cast (), detach : this );
3939 }
4040
4141 factory U8Array .fromList (List <int > data) {
@@ -48,11 +48,16 @@ class U8Array extends NativeArray<ffi.Uint8, int> {
4848
4949 U8Array .fromPointer (ffi.Pointer <ffi.Uint8 > ptr, int length) : super (length) {
5050 this .ptr = ptr;
51- finalizer.attach (this , ptr.cast ());
51+ finalizer.attach (this , ptr.cast (), detach : this );
5252 }
5353
5454 static final finalizer = ffi.NativeFinalizer (calloc.nativeFree);
5555
56+ void dispose () {
57+ finalizer.detach (this );
58+ calloc.free (ptr);
59+ }
60+
5661 @override
5762 void operator []= (int idx, int value) {
5863 // TODO: support negative index
@@ -79,7 +84,7 @@ class I8Array extends NativeArray<ffi.Int8, int> {
7984 ptr[idx] = value;
8085 }
8186 }
82- finalizer.attach (this , ptr.cast ());
87+ finalizer.attach (this , ptr.cast (), detach : this );
8388 }
8489
8590 factory I8Array .fromList (List <int > data) {
@@ -92,11 +97,16 @@ class I8Array extends NativeArray<ffi.Int8, int> {
9297
9398 I8Array .fromPointer (ffi.Pointer <ffi.Int8 > ptr, int length) : super (length) {
9499 this .ptr = ptr;
95- finalizer.attach (this , ptr.cast ());
100+ finalizer.attach (this , ptr.cast (), detach : this );
96101 }
97102
98103 static final finalizer = ffi.NativeFinalizer (calloc.nativeFree);
99104
105+ void dispose () {
106+ finalizer.detach (this );
107+ calloc.free (ptr);
108+ }
109+
100110 @override
101111 void operator []= (int idx, int value) {
102112 // TODO: support negative index
@@ -123,7 +133,7 @@ class U16Array extends NativeArray<ffi.Uint16, int> {
123133 ptr[idx] = value;
124134 }
125135 }
126- finalizer.attach (this , ptr.cast ());
136+ finalizer.attach (this , ptr.cast (), detach : this );
127137 }
128138
129139 factory U16Array .fromList (List <int > data) {
@@ -136,11 +146,16 @@ class U16Array extends NativeArray<ffi.Uint16, int> {
136146
137147 U16Array .fromPointer (ffi.Pointer <ffi.Uint16 > ptr, int length) : super (length) {
138148 this .ptr = ptr;
139- finalizer.attach (this , ptr.cast ());
149+ finalizer.attach (this , ptr.cast (), detach : this );
140150 }
141151
142152 static final finalizer = ffi.NativeFinalizer (calloc.nativeFree);
143153
154+ void dispose () {
155+ finalizer.detach (this );
156+ calloc.free (ptr);
157+ }
158+
144159 @override
145160 void operator []= (int idx, int value) {
146161 // TODO: support negative index
@@ -167,7 +182,7 @@ class I16Array extends NativeArray<ffi.Int16, int> {
167182 ptr[idx] = value;
168183 }
169184 }
170- finalizer.attach (this , ptr.cast ());
185+ finalizer.attach (this , ptr.cast (), detach : this );
171186 }
172187
173188 factory I16Array .fromList (List <int > data) {
@@ -180,11 +195,16 @@ class I16Array extends NativeArray<ffi.Int16, int> {
180195
181196 I16Array .fromPointer (ffi.Pointer <ffi.Int16 > ptr, int length) : super (length) {
182197 this .ptr = ptr;
183- finalizer.attach (this , ptr.cast ());
198+ finalizer.attach (this , ptr.cast (), detach : this );
184199 }
185200
186201 static final finalizer = ffi.NativeFinalizer (calloc.nativeFree);
187202
203+ void dispose () {
204+ finalizer.detach (this );
205+ calloc.free (ptr);
206+ }
207+
188208 @override
189209 void operator []= (int idx, int value) {
190210 // TODO: support negative index
@@ -211,7 +231,7 @@ class I32Array extends NativeArray<ffi.Int, int> {
211231 ptr[idx] = value;
212232 }
213233 }
214- finalizer.attach (this , ptr.cast ());
234+ finalizer.attach (this , ptr.cast (), detach : this );
215235 }
216236
217237 factory I32Array .fromList (List <int > data) {
@@ -224,11 +244,16 @@ class I32Array extends NativeArray<ffi.Int, int> {
224244
225245 I32Array .fromPointer (ffi.Pointer <ffi.Int > ptr, int length) : super (length) {
226246 this .ptr = ptr;
227- finalizer.attach (this , ptr.cast ());
247+ finalizer.attach (this , ptr.cast (), detach : this );
228248 }
229249
230250 static final finalizer = ffi.NativeFinalizer (calloc.nativeFree);
231251
252+ void dispose () {
253+ finalizer.detach (this );
254+ calloc.free (ptr);
255+ }
256+
232257 @override
233258 void operator []= (int idx, int value) {
234259 // TODO: support negative index
@@ -255,7 +280,7 @@ class F32Array extends NativeArray<ffi.Float, double> {
255280 ptr[idx] = value;
256281 }
257282 }
258- finalizer.attach (this , ptr.cast ());
283+ finalizer.attach (this , ptr.cast (), detach : this );
259284 }
260285
261286 factory F32Array .fromList (List <double > data) {
@@ -268,11 +293,16 @@ class F32Array extends NativeArray<ffi.Float, double> {
268293
269294 F32Array .fromPointer (ffi.Pointer <ffi.Float > ptr, int length) : super (length) {
270295 this .ptr = ptr;
271- finalizer.attach (this , ptr.cast ());
296+ finalizer.attach (this , ptr.cast (), detach : this );
272297 }
273298
274299 static final finalizer = ffi.NativeFinalizer (calloc.nativeFree);
275300
301+ void dispose () {
302+ finalizer.detach (this );
303+ calloc.free (ptr);
304+ }
305+
276306 @override
277307 void operator []= (int idx, double value) {
278308 // TODO: support negative index
@@ -299,7 +329,7 @@ class F64Array extends NativeArray<ffi.Double, double> {
299329 ptr[idx] = value;
300330 }
301331 }
302- finalizer.attach (this , ptr.cast ());
332+ finalizer.attach (this , ptr.cast (), detach : this );
303333 }
304334
305335 factory F64Array .fromList (List <double > data) {
@@ -312,11 +342,16 @@ class F64Array extends NativeArray<ffi.Double, double> {
312342
313343 F64Array .fromPointer (ffi.Pointer <ffi.Double > ptr, int length) : super (length) {
314344 this .ptr = ptr;
315- finalizer.attach (this , ptr.cast ());
345+ finalizer.attach (this , ptr.cast (), detach : this );
316346 }
317347
318348 static final finalizer = ffi.NativeFinalizer (calloc.nativeFree);
319349
350+ void dispose () {
351+ finalizer.detach (this );
352+ calloc.free (ptr);
353+ }
354+
320355 @override
321356 void operator []= (int idx, double value) {
322357 // TODO: support negative index
0 commit comments