Skip to content

Commit 75fd0bc

Browse files
committed
Keep reference to exectuor in execute context to prevent GC
1 parent de46365 commit 75fd0bc

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,11 @@ garrow_compute_initialize(GError **error)
274274
return garrow::check(error, status, "[compute][initialize]");
275275
}
276276

277-
typedef struct GArrowExecuteContextPrivate_
277+
struct GArrowExecuteContextPrivate
278278
{
279279
std::shared_ptr<arrow::compute::ExecContext> context;
280-
} GArrowExecuteContextPrivate;
280+
GArrowExecutor *executor;
281+
};
281282

282283
G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_OBJECT)
283284

@@ -289,6 +290,19 @@ enum {
289290
PROP_EXECUTOR = 1,
290291
};
291292

293+
static void
294+
garrow_execute_context_dispose(GObject *object)
295+
{
296+
auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object);
297+
298+
if (priv->executor) {
299+
g_object_unref(priv->executor);
300+
priv->executor = nullptr;
301+
}
302+
303+
G_OBJECT_CLASS(garrow_execute_context_parent_class)->dispose(object);
304+
}
305+
292306
static void
293307
garrow_execute_context_finalize(GObject *object)
294308
{
@@ -308,8 +322,8 @@ garrow_execute_context_set_property(GObject *object,
308322
switch (prop_id) {
309323
case PROP_EXECUTOR:
310324
{
311-
auto executor = GARROW_EXECUTOR(g_value_get_object(value));
312-
auto arrow_executor = garrow_executor_get_raw(executor);
325+
priv->executor = GARROW_EXECUTOR(g_value_dup_object(value));
326+
auto arrow_executor = garrow_executor_get_raw(priv->executor);
313327
priv->context =
314328
std::make_shared<arrow::compute::ExecContext>(arrow::default_memory_pool(),
315329
arrow_executor.get());
@@ -321,28 +335,55 @@ garrow_execute_context_set_property(GObject *object,
321335
}
322336
}
323337

338+
static void
339+
garrow_execute_context_get_property(GObject *object,
340+
guint prop_id,
341+
GValue *value,
342+
GParamSpec *pspec)
343+
{
344+
auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object);
345+
346+
switch (prop_id) {
347+
case PROP_EXECUTOR:
348+
g_value_set_object(value, priv->executor);
349+
break;
350+
default:
351+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
352+
break;
353+
}
354+
}
355+
324356
static void
325357
garrow_execute_context_init(GArrowExecuteContext *object)
326358
{
327359
auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object);
328-
priv->context = nullptr;
360+
new (&priv->context) std::shared_ptr<arrow::compute::ExecContext>;
329361
}
330362

331363
static void
332364
garrow_execute_context_class_init(GArrowExecuteContextClass *klass)
333365
{
334366
auto gobject_class = G_OBJECT_CLASS(klass);
335367

368+
gobject_class->dispose = garrow_execute_context_dispose;
336369
gobject_class->finalize = garrow_execute_context_finalize;
337370
gobject_class->set_property = garrow_execute_context_set_property;
371+
gobject_class->get_property = garrow_execute_context_get_property;
338372

339373
GParamSpec *spec;
374+
/**
375+
* GArrowExecuteContext:executor:
376+
*
377+
* The executor for execution.
378+
*
379+
* Since: 23.0.0
380+
*/
340381
spec = g_param_spec_object(
341382
"executor",
342383
"Executor",
343-
"The GArrowExecutor for execution",
384+
"The executor for execution",
344385
GARROW_TYPE_EXECUTOR,
345-
static_cast<GParamFlags>(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
386+
static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
346387
g_object_class_install_property(gobject_class, PROP_EXECUTOR, spec);
347388
}
348389

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestExecuteContext < Test::Unit::TestCase
19+
def setup
20+
@context = Arrow::ExecuteContext.new
21+
end
22+
23+
def test_executor
24+
assert_nil(@context.executor)
25+
26+
executor = Arrow::ThreadPool.new(1)
27+
context = Arrow::ExecuteContext.new(executor)
28+
assert_equal(executor, context.executor)
29+
end
30+
end

0 commit comments

Comments
 (0)