Skip to content

Commit 848b632

Browse files
authored
Use rcpputils::scope_exit in native getTopicNamesAndTypes Node method (#38)
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent cef12b3 commit 848b632

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

rcljava/src/main/cpp/org_ros2_rcljava_node_NodeImpl.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,28 +364,30 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeGetTopicNamesAndTypes(
364364
false,
365365
&topic_names_and_types);
366366
RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, "failed to get topic names and types");
367+
auto cleanup_names_and_types = rcpputils::make_scope_exit(
368+
[pnames_and_types = &topic_names_and_types, env]() {
369+
rcl_ret_t ret = rcl_names_and_types_fini(pnames_and_types);
370+
if (!env->ExceptionCheck() && RCL_RET_OK != ret) {
371+
rcljava_throw_rclexception(env, ret, "failed to fini topic names and types structure");
372+
}
373+
}
374+
);
367375

368376
for (size_t i = 0; i < topic_names_and_types.names.size; i++) {
369377
jobject jitem = env->NewObject(name_and_types_clazz, name_and_types_init_mid);
370-
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, goto cleanup);
378+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env);
371379
jstring jname = env->NewStringUTF(topic_names_and_types.names.data[i]);
372-
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, goto cleanup);
380+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env);
373381
env->SetObjectField(jitem, name_fid, jname);
374382
// the default constructor already inits types to an empty ArrayList
375383
jobject jtypes = env->GetObjectField(jitem, types_fid);
376384
for (size_t j = 0; j < topic_names_and_types.types[i].size; j++) {
377385
jstring jtype = env->NewStringUTF(topic_names_and_types.types[i].data[j]);
378386
env->CallBooleanMethod(jtypes, collection_add_mid, jtype);
379-
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, goto cleanup);
387+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env);
380388
}
381389
env->CallBooleanMethod(jnames_and_types, collection_add_mid, jitem);
382-
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, goto cleanup);
383-
}
384-
385-
cleanup:
386-
ret = rcl_names_and_types_fini(&topic_names_and_types);
387-
if (!env->ExceptionCheck() && RCL_RET_OK != ret) {
388-
rcljava_throw_rclexception(env, ret, "failed to fini topic names and types structure");
390+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env);
389391
}
390392
}
391393

0 commit comments

Comments
 (0)