Skip to content

Commit 2bd1e56

Browse files
committed
Use rcpputils::scope_exit in native getTopicNamesAndTypes Node method (osrf#38)
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent 860f319 commit 2bd1e56

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
@@ -352,28 +352,30 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeGetTopicNamesAndTypes(
352352
false,
353353
&topic_names_and_types);
354354
RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, "failed to get topic names and types");
355+
auto cleanup_names_and_types = rcpputils::make_scope_exit(
356+
[pnames_and_types = &topic_names_and_types, env]() {
357+
rcl_ret_t ret = rcl_names_and_types_fini(pnames_and_types);
358+
if (!env->ExceptionCheck() && RCL_RET_OK != ret) {
359+
rcljava_throw_rclexception(env, ret, "failed to fini topic names and types structure");
360+
}
361+
}
362+
);
355363

356364
for (size_t i = 0; i < topic_names_and_types.names.size; i++) {
357365
jobject jitem = env->NewObject(name_and_types_clazz, name_and_types_init_mid);
358-
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, goto cleanup);
366+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env);
359367
jstring jname = env->NewStringUTF(topic_names_and_types.names.data[i]);
360-
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, goto cleanup);
368+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env);
361369
env->SetObjectField(jitem, name_fid, jname);
362370
// the default constructor already inits types to an empty ArrayList
363371
jobject jtypes = env->GetObjectField(jitem, types_fid);
364372
for (size_t j = 0; j < topic_names_and_types.types[i].size; j++) {
365373
jstring jtype = env->NewStringUTF(topic_names_and_types.types[i].data[j]);
366374
env->CallBooleanMethod(jtypes, collection_add_mid, jtype);
367-
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, goto cleanup);
375+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env);
368376
}
369377
env->CallBooleanMethod(jnames_and_types, collection_add_mid, jitem);
370-
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, goto cleanup);
371-
}
372-
373-
cleanup:
374-
ret = rcl_names_and_types_fini(&topic_names_and_types);
375-
if (!env->ExceptionCheck() && RCL_RET_OK != ret) {
376-
rcljava_throw_rclexception(env, ret, "failed to fini topic names and types structure");
378+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env);
377379
}
378380
}
379381

0 commit comments

Comments
 (0)