@@ -237,18 +237,17 @@ inline bool startsWith(const std::string&completeString,
237
237
return (completeString.rfind (candidatePrefix, 0 ) == 0 );
238
238
}
239
239
240
- bool Handler::getDevicesAsPolyDriverList (const std::string& modelScopedName, yarp::dev::PolyDriverList& list)
240
+ bool Handler::getDevicesAsPolyDriverList (const std::string& modelScopedName, yarp::dev::PolyDriverList& list, std::vector<std::string>& deviceScopedNames )
241
241
{
242
+ deviceScopedNames.resize (0 );
243
+
242
244
list = yarp::dev::PolyDriverList ();
243
245
244
246
// This map contains only the yarpDeviceName that we actually added
245
247
// to the returned yarp::dev::PolyDriverList
246
248
std::unordered_map<std::string, std::string> inserted_yarpDeviceName2deviceDatabaseKey;
247
249
248
250
for (auto && devicesMapElem: m_devicesMap) {
249
- yDebug () << " DEBUG TO REMOVE: Add device deviceDatabaseKey " << devicesMapElem.first
250
- << " modelScopedName " << modelScopedName;
251
-
252
251
std::string deviceDatabaseKey = devicesMapElem.first ;
253
252
254
253
std::string yarpDeviceName;
@@ -267,7 +266,9 @@ bool Handler::getDevicesAsPolyDriverList(const std::string& modelScopedName, yar
267
266
// If no name collision is found, insert and continue
268
267
inserted_yarpDeviceName2deviceDatabaseKey.insert ({yarpDeviceName, deviceDatabaseKey});
269
268
list.push (devicesMapElem.second .object (), yarpDeviceName.c_str ());
270
- yDebug () << " add yarpDeviceName " << yarpDeviceName;
269
+ deviceScopedNames.push_back (deviceDatabaseKey);
270
+ // Increase usage counter
271
+ setDevice (deviceDatabaseKey, devicesMapElem.second .object ());
271
272
} else {
272
273
// If a name collision is found, print a clear error and return
273
274
yError () << " GazeboYARPPlugins robotinterface getDevicesAsPolyDriverList error: " ;
@@ -277,6 +278,8 @@ bool Handler::getDevicesAsPolyDriverList(const std::string& modelScopedName, yar
277
278
yError () << " Second instance: " << deviceDatabaseKey;
278
279
yError () << " Please eliminate or rename one of the two instances. " ;
279
280
list = yarp::dev::PolyDriverList ();
281
+ releaseDevicesInList (deviceScopedNames);
282
+ deviceScopedNames.resize (0 );
280
283
return false ;
281
284
}
282
285
@@ -288,4 +291,13 @@ bool Handler::getDevicesAsPolyDriverList(const std::string& modelScopedName, yar
288
291
}
289
292
290
293
294
+ void Handler::releaseDevicesInList (const std::vector<std::string>& deviceScopedNames)
295
+ {
296
+ for (auto && deviceScopedName: deviceScopedNames) {
297
+ removeDevice (deviceScopedName);
298
+ }
299
+ return ;
300
+ }
301
+
302
+
291
303
}
0 commit comments