This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Return templated local exception

Tags: None
(comma "," separated)
martinakos
Registered Member
Posts
53
Karma
0
OS

Return templated local exception

Sun Oct 13, 2013 4:10 pm
Hi,

I've created a "unique" (returns unique elements) helper function that works for a specific type ArrayXi but it throws an exception when I make the function "generic" (for one column or one row objects only).

This is the version that works:

Code: Select all
   inline Eigen::ArrayXi unique(const Eigen::ArrayXi &data)
   {
      Eigen::ArrayXi tmp(data);
      std::sort(tmp.data(), tmp.data()+tmp.size());
      int *it;
      it = std::unique(tmp.data(), tmp.data()+tmp.size());
      int d = std::distance(tmp.data(), it);
      tmp.conservativeResize(d);
      return tmp;
   }


this is my generic version:

Code: Select all
   template<class Derived>
   Eigen::DenseBase<Derived> unique(const Eigen::DenseBase<Derived> &data)
   {
      typename Derived::PlainObject tmp(data);
      std::sort(tmp.data(), tmp.data()+tmp.size());
      typename Derived::Scalar *it;
      it = std::unique(tmp.data(), tmp.data()+tmp.size());
      int d = std::distance(tmp.data(), it);
      tmp.conservativeResize(d);
      return tmp;
   }


I test the generic version with an ArrayXi

ArrayXi values;
ArrayXi unique_values;
//fill values
unique_values = unique(values);

everything goes well until it returns from unique. I would assume that ArrayXi copy constructor creates a new ArrayXi from tmp and that's what is assigned to unique_values. This test seems to work well for the function version with specific type ArrayXi but not for the generic version. In the generic version, somewhere when tmp comes out of scope the number of columns changes to a huge random number that ultimately throws Eigen::internal::throw_std_bad_alloc()

I don't know why is doing what is doing. Any help would be most appreciated.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Simply looking at your code, I don't have a clue. So I'd recommend to run your under valgrind to detect the memory issues.

As an optimization you could rather declare tmp as:

typename Derived::PlainObject tmp(data.derived());

but that should not solve your issue.
martinakos
Registered Member
Posts
53
Karma
0
OS

Re: Return templated local exception

Sun Oct 13, 2013 11:43 pm
Hi,

I tried the optimization using derived(), as you said no effect on the exception.
It's strange, it's like if the copy constructor for ArrayXi wasn't called at the return, I wonder if it has anything to do with the templated inheritance...

Using valgrind can be a bit difficult for me cause I'm on windows 7,
I could try to run it on a virtual machine, but I think it'll take me quite a lot of time as I have never use it before and I'm not very familiar with linux and gcc, gdb.

I think that for the moment I'll use specific type versions as I need to get this job done urgently.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Return templated local exception

Mon Oct 14, 2013 12:30 am
oh, I did not looked carefully but you cannot return a Eigen::DenseBase<Derived>. Such an object cannot exist alone. Instead the return type must be typename Derived::PlainObject.
martinakos
Registered Member
Posts
53
Karma
0
OS
Ok! thanks! that was it!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]