Skip to content

Commit

Permalink
Clean up memory by adding unregister method to CDev
Browse files Browse the repository at this point in the history
  • Loading branch information
jkflying committed Aug 7, 2019
1 parent e578b36 commit 535bbce
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
23 changes: 23 additions & 0 deletions src/lib/cdev/CDev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,27 @@ CDev::remove_poll_waiter(px4_pollfd_struct_t *fds)
return -EINVAL;
}

int CDev::unregister_driver_and_memory()
{
int retval = PX4_OK;

if (_registered) {
unregister_driver(_devname);
_registered = false;

} else {
retval = -ENODEV;
}

if (_devname != nullptr) {
free((void *)_devname);
_devname = nullptr;

} else {
retval = -ENODEV;
}

return retval;
}

} // namespace cdev
11 changes: 11 additions & 0 deletions src/lib/cdev/CDev.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ class __EXPORT CDev

px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */


/**
* First, unregisters the driver. Next, free the memory for the devname,
* in case it was expected to have ownership. Sets devname to nullptr.
*
* This is only needed if the ownership of the devname was passed to the CDev, otherwise ~CDev handles it.
*
* @return PX4_OK on success, -ENODEV if the devname is already nullptr
*/
int unregister_driver_and_memory();

private:
const char *_devname{nullptr}; /**< device node name */

Expand Down
9 changes: 2 additions & 7 deletions src/modules/uORB/uORBDeviceNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

#include "uORBDeviceNode.hpp"

#include "uORBDeviceNode.hpp"
#include "uORBUtils.hpp"
#include "uORBManager.hpp"

Expand Down Expand Up @@ -61,8 +60,7 @@ uORB::DeviceNode::DeviceNode(const struct orb_metadata *meta, const uint8_t inst
_meta(meta),
_instance(instance),
_priority(priority),
_queue_size(queue_size),
_devname(path)
_queue_size(queue_size)
{
}

Expand All @@ -72,10 +70,7 @@ uORB::DeviceNode::~DeviceNode()
delete[] _data;
}

if (_devname != nullptr) {
free((void *)_devname);
_devname = nullptr;
}
CDev::unregister_driver_and_memory();
}

int
Expand Down
1 change: 0 additions & 1 deletion src/modules/uORB/uORBDeviceNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ class uORB::DeviceNode : public cdev::CDev, public ListNode<uORB::DeviceNode *>
bool _published{false}; /**< has ever data been published */
uint8_t _queue_size; /**< maximum number of elements in the queue */
int8_t _subscriber_count{0};
const char *_devname; /**< keep this to delete at teardown, since CDev doesn't */

px4_task_t _publisher{0}; /**< if nonzero, current publisher. Only used inside the advertise call.
We allow one publisher to have an open file descriptor at the same time. */
Expand Down

0 comments on commit 535bbce

Please sign in to comment.