Skip to content

Avoid new and delete for internal data members of ObjectFactoryBase#3641

Merged
dzenanz merged 2 commits intoInsightSoftwareConsortium:masterfrom
N-Dekker:ObjectFactoryBase-unique_ptr
Sep 23, 2022
Merged

Avoid new and delete for internal data members of ObjectFactoryBase#3641
dzenanz merged 2 commits intoInsightSoftwareConsortium:masterfrom
N-Dekker:ObjectFactoryBase-unique_ptr

Conversation

@N-Dekker
Copy link
Copy Markdown
Contributor

@N-Dekker N-Dekker commented Sep 21, 2022

No description provided.

@github-actions github-actions Bot added the area:Core Issues affecting the Core module label Sep 21, 2022
Removed unnecessary dynamic memory allocation from internal factory lists.
@N-Dekker N-Dekker force-pushed the ObjectFactoryBase-unique_ptr branch from 33ffab5 to 09f6f8f Compare September 21, 2022 13:26
@N-Dekker N-Dekker changed the title Declare internal data members of ObjectFactoryBase as unique_ptr Avoid new and delete` for internal data members of ObjectFactoryBase Sep 21, 2022
@N-Dekker N-Dekker changed the title Avoid new and delete` for internal data members of ObjectFactoryBase Avoid new and delete for internal data members of ObjectFactoryBase Sep 21, 2022
Copy link
Copy Markdown
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

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

Did you figure out why heap allocation was used before?

{
itk::ObjectFactoryBase::UnRegisterAllFactories();
if (m_InternalFactories)
for (auto & m_InternalFactorie : m_InternalFactories)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

English spelling is Factory, not Factorie 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed @dzenanz The term m_InternalFactorie appears to be introduced with a code refactorieng ;-) PR #1628 commit 7f2c44c:

for (auto & m_InternalFactorie : *m_InternalFactories)
{
m_InternalFactorie->UnRegister();
}

Anyway, I'd rather not include fixes of old typos with this PR, though. These can be fixed later, IMhO. Unless you insist...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My check with

cd git/ITK
egrep -Ri 'factorie([^s]|$)'

shows the misspelling "factorie" occurs only in this variable in this file, on exactly two lines of code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I thought you are introducing this typo in this commit, due to Dutch spelling being Factorie 😄

Of course, it can be fixed later. Maybe a new STYLE commit as part of this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Leengit Thanks, but then you may also try for (const auto & m_ for similar typo's. 😺 (Remember we use the m_ prefix only for member variables.) Anyway, I'd rather defer such typo fixes to another PR, someone else, possibly.... unless you click "Request changes"!

Copy link
Copy Markdown
Contributor

@Leengit Leengit Sep 21, 2022

Choose a reason for hiding this comment

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

FWIW,

egrep -Ri 'for ?\((const )?auto ?&? ?[a-z_]*ie[a-z_]* '

returns only the case we know about.

(Edited to check for optional use of const and optional use of &.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, but for (const auto & m_ does occur twice anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For the issue of whether the looping variable should properly begin with m_, I find a total of three instances with egrep -Ri 'for ?\((const )?auto ?&? ?m_[a-z_]* ?':

Modules/IO/GDCM/src/itkGDCMSeriesFileNames.cxx:    for (const auto & m_InputFileName : m_InputFileNames)
Modules/Core/Common/src/itkObjectFactoryBase.cxx:      for (auto & m_InternalFactorie : *m_InternalFactories)
Modules/Core/Mesh/include/itkConnectedRegionsMeshFilter.hxx:    for (const auto & m_RegionSize : m_RegionSizes)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Leengit Would you possibly like to rename those three looping variables for us? 😃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've created Pull Request #3646.

@N-Dekker
Copy link
Copy Markdown
Contributor Author

Did you figure out why heap allocation was used before?

@dzenanz Good question 🤔 It appears that m_RegisteredFactories was declared as a pointer already with the first version of ObjectFactoryBase, as committed by Bill Hoffman (@billhoffman), 11 April 2000:

static std::list<itkObjectFactoryBase*>* m_RegisteredFactories;

You may ask Bill, but you know, at that time, raw pointers and manual memory management were very common! Everyone was using raw pointers around 2000! Maybe that could explain why...?

@N-Dekker N-Dekker marked this pull request as ready for review September 21, 2022 16:18
@dzenanz
Copy link
Copy Markdown
Member

dzenanz commented Sep 21, 2022

STL was not consistently implemented in all the compilers back then, so that might be the reason.

@dzenanz dzenanz merged commit 48d0cb0 into InsightSoftwareConsortium:master Sep 23, 2022
@dzenanz
Copy link
Copy Markdown
Member

dzenanz commented Sep 23, 2022

@Leengit @N-Dekker variable renames can go into another PR.

N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Sep 21, 2023
`ObjectFactoryBase::InitializeFactoryList()` has become unnecessary from
pull request InsightSoftwareConsortium#3641
commit 48d0cb0 "STYLE: Remove pointer
indirection from lists of ObjectFactoryBasePrivate"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Sep 21, 2023
`ObjectFactoryBase::InitializeFactoryList()` has become unnecessary from
pull request InsightSoftwareConsortium#3641
commit 48d0cb0 "STYLE: Remove pointer
indirection from lists of ObjectFactoryBasePrivate"
dzenanz pushed a commit that referenced this pull request Sep 26, 2023
`ObjectFactoryBase::InitializeFactoryList()` has become unnecessary from
pull request #3641
commit 48d0cb0 "STYLE: Remove pointer
indirection from lists of ObjectFactoryBasePrivate"
hjmjohnson pushed a commit to hjmjohnson/ITK that referenced this pull request May 6, 2026
`ObjectFactoryBase::InitializeFactoryList()` has become unnecessary from
pull request InsightSoftwareConsortium#3641
commit cf4e435 "STYLE: Remove pointer
indirection from lists of ObjectFactoryBasePrivate"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants