# C++11 VariableLengthVector with move

**URL:** https://discourse.itk.org/t/c-11-variablelengthvector-with-move/700
**Category:** Uncategorized
**Created:** [February 22, 2018, 8:35pm UTC](https://discourse.itk.org/t/c-11-variablelengthvector-with-move/700 "2018-02-22T20:35:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [February 22, 2018, 8:35pm UTC](https://discourse.itk.org/t/c-11-variablelengthvector-with-move/700/1 "2018-02-22T20:35:14Z")

</div>

I am just tracking down a segmentation fault I am encountering wth the [ITKTextureFeature](https://github.com/InsightSoftwareConsortium/ITKTextureFeatures) modules instantiated with VectorImages.

I am tracking it down to using move operations of the VariableLengthVector:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkVariableLengthVector.hxx#L90-L128>

It appears related to previously a deep copy was done, but now the move assignment is keeping the data to memory which has been released for a “GetPixel” type operation… Still looking deeper…

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [February 23, 2018, 1:28pm UTC](https://discourse.itk.org/t/c-11-variablelengthvector-with-move/700/2 "2018-02-23T13:28:00Z")

</div>

Maybe here?:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkVectorImage.h#L243-L259>

And the constructor here, defaults to `m_LetArrayManageMemory(false)`

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkVariableLengthVector.h#L328-L342>

So I think it could be solved if the move constructor does not modify `v.m_LetArrayManageMemory`?

so:

```auto
template< typename TValue >
VariableLengthVector< TValue >
::VariableLengthVector(Self && v) noexcept
: m_LetArrayManageMemory(v.m_LetArrayManageMemory)
, m_Data (v.m_Data)
, m_NumElements (v.m_NumElements)
{
// Do not modify v.m_LetArrayManageMemory
v.m_Data = nullptr;
v.m_NumElements = 0;
}

```

Not 100% sure though.

Or even, set it to `false` directly?

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [February 26, 2018, 7:33pm UTC](https://discourse.itk.org/t/c-11-variablelengthvector-with-move/700/3 "2018-02-26T19:33:05Z")

</div>

I found the solution to my problem and made a pull request here:  
[https://github.com/InsightSoftwareConsortium/ITKTextureFeatures/pull/53](https://github.com/InsightSoftwareConsortium/ITKTextureFeatures/pull/53)

The assignment operator very nicely separate out the cases:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkVariableLengthVector.hxx#L102-L143>

There may be some things suspicious with the move constructor. But I think it’s OK for the rvalue reference to be set to manage it’s own memory and a `nullptr`. The problem I see is what is the intent of the following code:

```auto
VariableLengthVectorType v = vector_image->GetPixelIndex(idx);

```

The constructor vs the assignment operator have different behavior. This is actually a call to the move constructor, where `v` with reference the image buffer. But if it was a call to the assignment operator, the vector v, would not be a proxy, and would have it’s own memory, so it would be a deep copy. This may result in un intended aliasing.

This is a complicated situation that needs a little more examination and verification.
