-
Notifications
You must be signed in to change notification settings - Fork 708
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
Fix a memory leak in AlignedVector. #13005
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// -------------------------------------------------------------------------------------------- | ||
// | ||
// Copyright (C) 2021 by the deal.II authors | ||
// | ||
// This file is part of the deal.II library. | ||
// | ||
// The deal.II library is free software; you can use it, redistribute | ||
// it, and/or modify it under the terms of the GNU Lesser General | ||
// Public License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// The full text of the license can be found in the file LICENSE.md at | ||
// the top level directory of deal.II. | ||
// | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
|
||
// Check for a bug in memory management in class Table (which uses | ||
// AlignedVector) that had crept in at some point. | ||
|
||
#include <deal.II/base/logstream.h> | ||
#include <deal.II/base/table.h> | ||
|
||
#include "../tests.h" | ||
|
||
|
||
int object_number = 0; | ||
int objects_destroyed = 0; | ||
|
||
class C | ||
{ | ||
public: | ||
C() | ||
{ | ||
object_number = ::object_number++; | ||
deallog << "default constructor. Object number " << object_number | ||
<< std::endl; | ||
} | ||
|
||
C(const C &c) | ||
{ | ||
object_number = ::object_number++; | ||
deallog << "copy constructor from " << c.object_number << ". Object number " | ||
<< object_number << std::endl; | ||
} | ||
|
||
C(const C &&c) | ||
{ | ||
object_number = ::object_number++; | ||
deallog << "move constructor from " << c.object_number << ". Object number " | ||
<< object_number << std::endl; | ||
} | ||
|
||
C & | ||
operator=(const C &c) | ||
{ | ||
deallog << "copy operator called for " << object_number << " <- " | ||
<< c.object_number << std::endl; | ||
return *this; | ||
} | ||
|
||
C & | ||
operator=(const C &&c) | ||
{ | ||
deallog << "move operator called for " << object_number << " <- std::move(" | ||
<< c.object_number << ')' << std::endl; | ||
return *this; | ||
} | ||
|
||
|
||
~C() | ||
{ | ||
deallog << "destructor. Object number " << object_number << std::endl; | ||
++objects_destroyed; | ||
} | ||
|
||
private: | ||
unsigned int object_number; | ||
}; | ||
|
||
|
||
|
||
void | ||
test() | ||
{ | ||
deallog << "---- Creating outer table" << std::endl; | ||
Table<1, C> table({1}); | ||
|
||
// Copy the object, then destroy the copy again. | ||
{ | ||
deallog << "---- Cloning outer table" << std::endl; | ||
Table<1, C> x(table); | ||
deallog << "---- Destroying the clone" << std::endl; | ||
} | ||
|
||
deallog << "---- Destroying the source table" << std::endl; | ||
} | ||
|
||
|
||
|
||
int | ||
main(int argc, char **argv) | ||
{ | ||
initlog(); | ||
|
||
test(); | ||
|
||
deallog << "Objects created: " << object_number << std::endl; | ||
deallog << "Objects destroyed: " << objects_destroyed << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
DEAL::---- Creating outer table | ||
DEAL::default constructor. Object number 0 | ||
DEAL::---- Cloning outer table | ||
DEAL::default constructor. Object number 1 | ||
DEAL::destructor. Object number 1 | ||
DEAL::copy constructor from 0. Object number 2 | ||
DEAL::---- Destroying the clone | ||
DEAL::destructor. Object number 2 | ||
DEAL::---- Destroying the source table | ||
DEAL::destructor. Object number 0 | ||
DEAL::Objects created: 3 | ||
DEAL::Objects destroyed: 3 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bug is here: When we call
resize_fast()
, we allocate memory and default construct objects in that memory. But in the line below (AlignedVectorCopyConstruct
) we copy-construct other objects in that same memory location, forgetting to call the destructor on what we had done before.The solution is to not initialize the memory we allocate -- that's what
reserve()
does in contrast toresize*()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, on second look, I see that in essence the same code I just wrote here is also what's in the copy constructor just a few lines up.