File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed
mediapipe-task-text/lib/src/tasks/text_classification Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 1
1
import 'dart:ffi' ;
2
+ import 'package:ffi/ffi.dart' ;
2
3
import 'package:mediapipe_core/mediapipe_core.dart' ;
3
4
import 'mediapipe_common_bindings.dart' as bindings;
4
5
@@ -34,6 +35,17 @@ class Category {
34
35
);
35
36
}
36
37
38
+ static void freeStructs (Pointer <bindings.Category > structs, int count) {
39
+ int index = 0 ;
40
+ while (index < count) {
41
+ bindings.Category obj = structs[index];
42
+ calloc.free (obj.category_name);
43
+ calloc.free (obj.display_name);
44
+ index++ ;
45
+ }
46
+ calloc.free (structs);
47
+ }
48
+
37
49
@override
38
50
String toString () => 'Category(index=$index , score=$score , '
39
51
'categoryName=$categoryName , displayName=$displayName )' ;
@@ -71,6 +83,20 @@ class Classifications {
71
83
);
72
84
}
73
85
86
+ static void freeStructs (
87
+ Pointer <bindings.Classifications > structs,
88
+ int count,
89
+ ) {
90
+ int index = 0 ;
91
+ while (index < count) {
92
+ bindings.Classifications obj = structs[index];
93
+ Category .freeStructs (obj.categories, obj.categories_count);
94
+ calloc.free (obj.head_name);
95
+ index++ ;
96
+ }
97
+ calloc.free (structs);
98
+ }
99
+
74
100
Category ? get firstCategory =>
75
101
categories.isNotEmpty ? categories.first : null ;
76
102
Original file line number Diff line number Diff line change
1
+ import 'dart:ffi' ;
2
+ import 'package:ffi/ffi.dart' ;
1
3
import 'package:mediapipe_core/mediapipe_core.dart' ;
2
4
import 'package:mediapipe_text/src/mediapipe_text_bindings.dart' as bindings;
3
5
@@ -32,6 +34,14 @@ class TextClassifierResult {
32
34
);
33
35
}
34
36
37
+ static void freeStruct (Pointer <bindings.TextClassifierResult > struct) {
38
+ Classifications .freeStructs (
39
+ struct.ref.classifications,
40
+ struct.ref.classifications_count,
41
+ );
42
+ calloc.free (struct);
43
+ }
44
+
35
45
Classifications ? get firstClassification =>
36
46
classifications.isNotEmpty ? classifications.first : null ;
37
47
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ class TextClassifier extends BaseTextClassifier {
40
40
final result = TextClassifierResult .fromStruct (resultsPtr.ref);
41
41
_log.fine ('Text classification result: $result ' );
42
42
_bindings.text_classifier_close (classifierPtr);
43
- calloc. free (resultsPtr);
43
+ TextClassifierResult . freeStruct (resultsPtr);
44
44
return result;
45
45
}
46
46
}
You can’t perform that action at this time.
0 commit comments