@@ -39,11 +39,7 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeCreatePublisherHandle(
3939 jmethodID mid = env->GetStaticMethodID (jmessage_class, " getTypeSupport" , " ()J" );
4040 jlong jts = env->CallStaticLongMethod (jmessage_class, mid);
4141
42- const char * topic_tmp = env->GetStringUTFChars (jtopic, 0 );
43-
44- std::string topic (topic_tmp);
45-
46- env->ReleaseStringUTFChars (jtopic, topic_tmp);
42+ const char * topic = env->GetStringUTFChars (jtopic, 0 );
4743
4844 rcl_node_t * node = reinterpret_cast <rcl_node_t *>(node_handle);
4945
@@ -56,7 +52,8 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeCreatePublisherHandle(
5652 rmw_qos_profile_t * qos_profile = reinterpret_cast <rmw_qos_profile_t *>(qos_profile_handle);
5753 publisher_ops.qos = *qos_profile;
5854
59- rcl_ret_t ret = rcl_publisher_init (publisher, node, ts, topic.c_str (), &publisher_ops);
55+ rcl_ret_t ret = rcl_publisher_init (publisher, node, ts, topic, &publisher_ops);
56+ env->ReleaseStringUTFChars (jtopic, topic);
6057
6158 if (ret != RCL_RET_OK) {
6259 std::string msg = " Failed to create publisher: " + std::string (rcl_get_error_string ().str );
@@ -77,11 +74,7 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeCreateSubscriptionHandle(
7774 jmethodID mid = env->GetStaticMethodID (jmessage_class, " getTypeSupport" , " ()J" );
7875 jlong jts = env->CallStaticLongMethod (jmessage_class, mid);
7976
80- const char * topic_tmp = env->GetStringUTFChars (jtopic, 0 );
81-
82- std::string topic (topic_tmp);
83-
84- env->ReleaseStringUTFChars (jtopic, topic_tmp);
77+ const char * topic = env->GetStringUTFChars (jtopic, 0 );
8578
8679 rcl_node_t * node = reinterpret_cast <rcl_node_t *>(node_handle);
8780
@@ -95,7 +88,8 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeCreateSubscriptionHandle(
9588 rmw_qos_profile_t * qos_profile = reinterpret_cast <rmw_qos_profile_t *>(qos_profile_handle);
9689 subscription_ops.qos = *qos_profile;
9790
98- rcl_ret_t ret = rcl_subscription_init (subscription, node, ts, topic.c_str (), &subscription_ops);
91+ rcl_ret_t ret = rcl_subscription_init (subscription, node, ts, topic, &subscription_ops);
92+ env->ReleaseStringUTFChars (jtopic, topic);
9993
10094 if (ret != RCL_RET_OK) {
10195 std::string msg = " Failed to create subscription: " + std::string (rcl_get_error_string ().str );
@@ -121,11 +115,7 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeCreateServiceHandle(
121115
122116 assert (jts != 0 );
123117
124- const char * service_name_tmp = env->GetStringUTFChars (jservice_name, 0 );
125-
126- std::string service_name (service_name_tmp);
127-
128- env->ReleaseStringUTFChars (jservice_name, service_name_tmp);
118+ const char * service_name = env->GetStringUTFChars (jservice_name, 0 );
129119
130120 rcl_node_t * node = reinterpret_cast <rcl_node_t *>(node_handle);
131121
@@ -138,7 +128,8 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeCreateServiceHandle(
138128 rmw_qos_profile_t * qos_profile = reinterpret_cast <rmw_qos_profile_t *>(qos_profile_handle);
139129 service_ops.qos = *qos_profile;
140130
141- rcl_ret_t ret = rcl_service_init (service, node, ts, service_name.c_str (), &service_ops);
131+ rcl_ret_t ret = rcl_service_init (service, node, ts, service_name, &service_ops);
132+ env->ReleaseStringUTFChars (jservice_name, service_name);
142133
143134 if (ret != RCL_RET_OK) {
144135 std::string msg = " Failed to create service: " + std::string (rcl_get_error_string ().str );
@@ -164,11 +155,7 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeCreateClientHandle(
164155
165156 assert (jts != 0 );
166157
167- const char * service_name_tmp = env->GetStringUTFChars (jservice_name, 0 );
168-
169- std::string service_name (service_name_tmp);
170-
171- env->ReleaseStringUTFChars (jservice_name, service_name_tmp);
158+ const char * service_name = env->GetStringUTFChars (jservice_name, 0 );
172159
173160 rcl_node_t * node = reinterpret_cast <rcl_node_t *>(node_handle);
174161
@@ -181,7 +168,8 @@ Java_org_ros2_rcljava_node_NodeImpl_nativeCreateClientHandle(
181168 rmw_qos_profile_t * qos_profile = reinterpret_cast <rmw_qos_profile_t *>(qos_profile_handle);
182169 client_ops.qos = *qos_profile;
183170
184- rcl_ret_t ret = rcl_client_init (client, node, ts, service_name.c_str (), &client_ops);
171+ rcl_ret_t ret = rcl_client_init (client, node, ts, service_name, &client_ops);
172+ env->ReleaseStringUTFChars (jservice_name, service_name);
185173
186174 if (ret != RCL_RET_OK) {
187175 std::string msg = " Failed to create client: " + std::string (rcl_get_error_string ().str );
0 commit comments