Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove memory leaks #186

Open
wants to merge 8 commits into
base: rolling
Choose a base branch
from
Open

Remove memory leaks #186

wants to merge 8 commits into from

Conversation

ahcorde
Copy link

@ahcorde ahcorde commented Jun 24, 2021

Signed-off-by: ahcorde [email protected]

I ran class_loader with asan and I found some memory leaks.

There is still one memory leak when we create unmanaged instance, the way to reproduce this leak is to run basicLoadUnmanaged and noWarningOnLazyLoad tests at the same time.

what is the right way to destroy this memory ?

@ahcorde ahcorde requested a review from hidmic June 24, 2021 11:06
@ahcorde ahcorde self-assigned this Jun 24, 2021
unloadLibraryInternal(false);
}
if (remove_when_possible) {
delete this;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahcorde this is very unusual. I'd suggest we use an std::shared_ptr to track ClassLoader ownership and defer cleanup tasks to its destructor.

@@ -169,6 +169,7 @@ class ClassLoader
template<class Base>
Base * createUnmanagedInstance(const std::string & derived_class_name)
{
class_loader_ref_count--;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahcorde to compensate for the ref count increase in createRawInstance? What if a plugin is simultaneously deleted? I'd strongly suggest not creating these glitches in the ref count.

@@ -545,6 +545,7 @@ void unloadLibrary(const std::string & library_path, ClassLoader * loader)
", keeping library %s open.",
library_path.c_str());
}
purgeGraveyardOfMetaobjects(library_path, loader, true);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahcorde hmm, I think this was not here on purpose:

// Insert into graveyard
// We remove the metaobject from its factory map, but we don't destroy it...instead it
// saved to a "graveyard" to the side.
// This is due to our static global variable initialization problem that causes factories
// to not be registered when a library is closed and then reopened.
// This is because it's truly not closed due to the use of global symbol binding i.e.
// calling dlopen with RTLD_GLOBAL instead of RTLD_LOCAL.
// We require using the former as the which is required to support RTTI

But I wonder why

TEST(ClassLoaderUniquePtrTest, loadRefCountingLazy) {
isn't failing for you though.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this @ahcorde ?

@ahcorde
Copy link
Author

ahcorde commented Jun 25, 2021

--packages-above-and-dependencies class_loader

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: ahcorde <[email protected]>
@ahcorde ahcorde requested a review from hidmic June 25, 2021 09:12
include/class_loader/class_loader.hpp Outdated Show resolved Hide resolved
include/class_loader/class_loader.hpp Outdated Show resolved Hide resolved
Signed-off-by: ahcorde <[email protected]>
@ahcorde ahcorde requested a review from hidmic June 25, 2021 14:11
@ahcorde ahcorde requested a review from hidmic September 17, 2021 14:38
Signed-off-by: ahcorde <[email protected]>
Copy link

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once comments are addressed and CI goes green, I'm onboard with the change.

Signed-off-by: ahcorde <[email protected]>
@ahcorde ahcorde requested a review from hidmic September 20, 2021 16:09
Copy link

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uncrustify is angry again

include/class_loader/class_loader.hpp Outdated Show resolved Hide resolved
Signed-off-by: ahcorde <[email protected]>
@ahcorde ahcorde requested a review from hidmic September 21, 2021 08:51
@ahcorde
Copy link
Author

ahcorde commented Sep 21, 2021

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: ahcorde <[email protected]>
@ahcorde
Copy link
Author

ahcorde commented Oct 26, 2021

Rerunning tests. Anyhow failures look unrelared.

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Copy link

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahcorde LGTM but for a question. Also, we should probably open a ticket to come back after Humble and remove the raw pointer interface.

Comment on lines +78 to +79
* This class inherit from enable_shared_from_this which means we must used smart pointers,
* otherwise the code might work but it may run into a leak.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahcorde nit:

Suggested change
* This class inherit from enable_shared_from_this which means we must used smart pointers,
* otherwise the code might work but it may run into a leak.
* This class inherits from `std::enable_shared_from_this`. It should be instantiated using an `std::shared_ptr`.
* Code will otherwise work but memory may be leaked.

@@ -545,6 +545,7 @@ void unloadLibrary(const std::string & library_path, ClassLoader * loader)
", keeping library %s open.",
library_path.c_str());
}
purgeGraveyardOfMetaobjects(library_path, loader, true);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this @ahcorde ?

Comment on lines +74 to +76
for (unsigned int i = 0; i < impl_->associated_class_loaders_.size(); i++) {
delete impl_->associated_class_loaders_[i];
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this doesn't cause problems unless these AbstractMetaObjectBase are never destroyed while the program is running?

Are these not the same ClassLoaders you are storing in shared_ptr objects?

@audrow audrow changed the base branch from ros2 to rolling June 28, 2022 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants