Question

I'm creating a Matrix with the Map class:

float* d = new float[rows*cols];
// ... getting data into d
Eigen::Map<Eigen::MatrixXf>(d, rows, cols);         

My question is - does Map take ownership of the data pointer and deletes it when its done? or does it copy the data and should I free it myself after the Map is created?

Thanks.

Was it helpful?

Solution 2

Documentation is very sparse on this but apart from that it's more common to not delete what you have not created, posts like this one suggest you should delete d when you're ready.

I suggest to run a memory profiler like valgrind which would tell you in case the pointer has not been deleted.

In case you don't delete d valgrind reports:

400 bytes in 1 blocks are definitely lost in loss record 1 of 1
  in operator new[](unsigned long) in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
  1: operator new[](unsigned long) in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
  2: main in test

So you can be quite sure you have to delete your data (which is good).

Shame on that documentation...

OTHER TIPS

No, Map does not take ownership so you are still responsible for freeing memory. Actually, the contrary would be impossible for several reasons:

  • Map cannot know how the memory has been allocated
  • You can map only a subrange of an allocated buffer

Moreover, the following would be very strange:

 float *d = new float[10];
 // ...
 VectorXf v = ...;
 v = v + Map<VectorXf>(d,10);
 // now d is a dead pointer

No way!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top