@@ -70,6 +70,7 @@ func queryFinalizer(query *Query) {
70
70
71
71
// The native query object in the ObjectBox core is not tied with other resources.
72
72
// Thus timing of the Close call is independent from other resources.
73
+ // Warning: it's important the object is kept around until a native call returns, e.g. using `runtime.KeepAlive(query)`.
73
74
func (query * Query ) installFinalizer () {
74
75
runtime .SetFinalizer (query , queryFinalizer )
75
76
}
@@ -80,6 +81,8 @@ func (query *Query) errorClosed() error {
80
81
81
82
// Find returns all objects matching the query
82
83
func (query * Query ) Find () (objects interface {}, err error ) {
84
+ defer runtime .KeepAlive (query )
85
+
83
86
if query .cQuery == nil {
84
87
return 0 , query .errorClosed ()
85
88
}
@@ -113,6 +116,8 @@ func (query *Query) Limit(limit uint64) *Query {
113
116
114
117
// FindIds returns IDs of all objects matching the query
115
118
func (query * Query ) FindIds () ([]uint64 , error ) {
119
+ defer runtime .KeepAlive (query )
120
+
116
121
if query .cQuery == nil {
117
122
return nil , query .errorClosed ()
118
123
}
@@ -137,6 +142,7 @@ func (query *Query) Count() (uint64, error) {
137
142
if err := cCall (func () C.obx_err { return C .obx_query_count (query .cQuery , & cResult ) }); err != nil {
138
143
return 0 , err
139
144
}
145
+ runtime .KeepAlive (query )
140
146
return uint64 (cResult ), nil
141
147
}
142
148
@@ -155,6 +161,8 @@ func (query *Query) Remove() (count uint64, err error) {
155
161
if err := cCall (func () C.obx_err { return C .obx_query_remove (query .cQuery , & cResult ) }); err != nil {
156
162
return 0 , err
157
163
}
164
+
165
+ runtime .KeepAlive (query )
158
166
return uint64 (cResult ), nil
159
167
}
160
168
@@ -163,9 +171,11 @@ func (query *Query) DescribeParams() (string, error) {
163
171
if query .cQuery == nil {
164
172
return "" , query .errorClosed ()
165
173
}
174
+
166
175
// no need to free, it's handled by the cQuery internally
167
176
cResult := C .obx_query_describe_params (query .cQuery )
168
177
178
+ runtime .KeepAlive (query )
169
179
return C .GoString (cResult ), nil
170
180
}
171
181
@@ -205,6 +215,8 @@ type propertyOrAlias interface {
205
215
206
216
// SetStringParams changes query parameter values on the given property
207
217
func (query * Query ) SetStringParams (identifier propertyOrAlias , values ... string ) error {
218
+ defer runtime .KeepAlive (query )
219
+
208
220
if err := query .checkIdentifier (identifier ); err != nil {
209
221
return err
210
222
}
@@ -236,6 +248,8 @@ func (query *Query) SetStringParams(identifier propertyOrAlias, values ...string
236
248
237
249
// SetStringParamsIn changes query parameter values on the given property
238
250
func (query * Query ) SetStringParamsIn (identifier propertyOrAlias , values ... string ) error {
251
+ defer runtime .KeepAlive (query )
252
+
239
253
if err := query .checkIdentifier (identifier ); err != nil {
240
254
return err
241
255
}
@@ -263,6 +277,8 @@ func (query *Query) SetStringParamsIn(identifier propertyOrAlias, values ...stri
263
277
264
278
// SetInt64Params changes query parameter values on the given property
265
279
func (query * Query ) SetInt64Params (identifier propertyOrAlias , values ... int64 ) error {
280
+ defer runtime .KeepAlive (query )
281
+
266
282
if err := query .checkIdentifier (identifier ); err != nil {
267
283
return err
268
284
}
@@ -299,6 +315,8 @@ func (query *Query) SetInt64Params(identifier propertyOrAlias, values ...int64)
299
315
300
316
// SetInt64ParamsIn changes query parameter values on the given property
301
317
func (query * Query ) SetInt64ParamsIn (identifier propertyOrAlias , values ... int64 ) error {
318
+ defer runtime .KeepAlive (query )
319
+
302
320
if err := query .checkIdentifier (identifier ); err != nil {
303
321
return err
304
322
}
@@ -323,6 +341,8 @@ func (query *Query) SetInt64ParamsIn(identifier propertyOrAlias, values ...int64
323
341
324
342
// SetInt32ParamsIn changes query parameter values on the given property
325
343
func (query * Query ) SetInt32ParamsIn (identifier propertyOrAlias , values ... int32 ) error {
344
+ defer runtime .KeepAlive (query )
345
+
326
346
if err := query .checkIdentifier (identifier ); err != nil {
327
347
return err
328
348
}
@@ -347,6 +367,8 @@ func (query *Query) SetInt32ParamsIn(identifier propertyOrAlias, values ...int32
347
367
348
368
// SetFloat64Params changes query parameter values on the given property
349
369
func (query * Query ) SetFloat64Params (identifier propertyOrAlias , values ... float64 ) error {
370
+ defer runtime .KeepAlive (query )
371
+
350
372
if err := query .checkIdentifier (identifier ); err != nil {
351
373
return err
352
374
}
@@ -384,6 +406,8 @@ func (query *Query) SetFloat64Params(identifier propertyOrAlias, values ...float
384
406
385
407
// SetBytesParams changes query parameter values on the given property
386
408
func (query * Query ) SetBytesParams (identifier propertyOrAlias , values ... []byte ) error {
409
+ defer runtime .KeepAlive (query )
410
+
387
411
if err := query .checkIdentifier (identifier ); err != nil {
388
412
return err
389
413
}
0 commit comments