Remove AllocateElements from VariableLengthVector#5710
Conversation
| : m_Data(nullptr) | ||
| : m_Data(new TValue[length]) | ||
| { | ||
| Reserve(length); |
There was a problem hiding this comment.
Simplified the VariableLengthVector(length) constructor by removing its internal Reserve(length) call.
It appears unnecessary for VariableLengthVector to replace exceptions from `new T[n]` with ITK exceptions. This commit also simplified the `VariableLengthVector(length)` constructor.
Although AllocateElements is a public member function of VariableLengthVector, it was only used internally, by VariableLengthVector itself.
a222c4d to
f080d4f
Compare
blowekamp
left a comment
There was a problem hiding this comment.
These changes look good to me.
|
Valgrind seems to be complaining |
Thanks @issakomi, but as far as I can see, those failures are unrelated to this PR (Remove AllocateElements from VariableLengthVector). I see, https://open.cdash.org/builds/10917349/dynamic_analysis/11341974 says:
|
MaskedAssignFixture.SetGetPrint failed, I am talking about other tests with Valgring defects: |
|
Ah, thanks @issakomi I see now, when the copy-constructor of VariableLengthVector copies a VariableLengthVector of length zero, it calls I think it would be better if |
Originally, a zero-length VariableLengthVector might still have its `m_Data` pointing to the heap, as it might have its memory allocated by `new TValue[length]`, with `length == 0`. A memory leak in `VariableLengthVector(unsigned int length)` was then introduced by pull request InsightSoftwareConsortium#5710 commit bb79801 "STYLE: Replace AllocateElements calls within VariableLengthVector" forgetting to `delete` memory that was allocated by `new TValue[0]`. As was reported by Mihail Isakov, who observed, pointing to the itk-valgrind results at https://open.cdash.org/builds/10917349/dynamic_analysis With this commit, m_Data should always be null when the VariableLengthVector is empty and manages its own memory (m_LetArrayManageMemory is `true`).
Originally, a zero-length VariableLengthVector might still have its `m_Data` pointing to the heap, as it might have its memory allocated by `new TValue[length]`, with `length == 0`. A memory leak in `VariableLengthVector(unsigned int length)` was then introduced by pull request InsightSoftwareConsortium#5710 commit bb79801 "STYLE: Replace AllocateElements calls within VariableLengthVector" forgetting to `delete` memory that was allocated by `new TValue[0]`. As was reported by Mihail Isakov, who observed, pointing to the itk-valgrind results at https://open.cdash.org/builds/10917349/dynamic_analysis With this commit, m_Data should always be null when the VariableLengthVector is empty and manages its own memory (m_LetArrayManageMemory is `true`).
Originally, a zero-length VariableLengthVector might still have its `m_Data` pointing to the heap, as it might have its memory allocated by `new TValue[length]`, with `length == 0`. A memory leak in `VariableLengthVector(unsigned int length)` was then introduced by pull request InsightSoftwareConsortium#5710 commit bb79801 "STYLE: Replace AllocateElements calls within VariableLengthVector" forgetting to `delete` memory that was allocated by `new TValue[0]`. As was reported by Mihail Isakov, referring to the Valgrind results at https://open.cdash.org/builds/10917349/dynamic_analysis With this commit, m_Data should always be null when the VariableLengthVector is empty and manages its own memory (m_LetArrayManageMemory is `true`).
VariableLengthVector::AllocateElementsjust converted any possible exception fromnew TValue[size]to an ITK exception:ITK/Modules/Core/Common/include/itkVariableLengthVector.hxx
Lines 204 to 216 in d66197a
It appears unnecessary for VariableLengthVector to do so. In many other cases, ITK does allow non-ITK exceptions to occur, so end-users have to deal with them anyway.
This pull request was triggered by a comment by Bradley (@blowekamp) at #5693 (comment) saying: