Jump to content
  • Advertisement
Sign in to follow this  
Odoacer

pointers to member functions in STL lists

This topic is 4749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, that title was a mouthful, but hopefully it's self-explanatory. Basically, I'm getting a coredump/seg fault/what-have-you-it (under Solaris 9/g++) while trying to iterate over a list containing pointers to member functions. Does anyone know what may be the cause/ how to fix it? Here's some relevant code, hope it helps:
class Foo{

  list[int(Foo::*ptr)()] getList(){ return bar; }

  int baz();
  int bat();

  Foo(){
    bar.push_front(&Foo::baz);
    bar.push_front(&Foo::bat);
  }
  private:
    list[int(Foo::*ptr)()] bar;
};

main(){
  Foo f;
  typedef int(Foo::*ptr)();
  for(list[ptr]::iterator i = f.getList().begin(); i != f.getList().end(); i++){
    (f.**i)();  //code fails here on the second time through the loop (first function is called successfully)
  }
}


(edited to put code in codebox, note: replace occurences of [ ] with < >)

Share this post


Link to post
Share on other sites
Advertisement

class Foo{

list<int(Foo::*ptr)()> getList(){ return bar; }

int baz();
int bat();

Foo(){
bar.push_front(&Foo::baz);
bar.push_front(&Foo::bat);
}
private:
list<int(Foo::*ptr)()> bar;
};

main(){
Foo f;
typedef int(Foo::*ptr)();
for(list<ptr>::iterator i = f.getList().begin(); i != f.getList().end(); i++){
(f.**i)(); //code fails here on the second time through the loop (first function is called successfully)
}
}


I got rid of the &lt;s and &gt;s

Hmm, that is quite complicated.

1. Are you sure you want getList() to return a copy of the list? That would require copying the the pointer-to-member-functions... which might cause problems... I don't know. I think returning a reference or a const reference would be better (Unless you have a reason for copying it)

Share this post


Link to post
Share on other sites
Ohh wait a second...

for(list<ptr>::iterator i = f.getList().begin(); i != f.getList().end(); i++){

If you're calling getList twice you're using two different copies of the list... I think... so you'd end up comparing an iterator from one list with the end of another list

Share this post


Link to post
Share on other sites
I don't know much about the subject, but you may want to look into using boost::function, it makes using function pointers (including pointer to member) much easier and safer.

Share this post


Link to post
Share on other sites
I believe what is happening is that when you return the list by value, you get an iterator to the start of the copied list. The copy of list then goes out of scope, deleting the list, killing the nodes and rendering the node pointers garbage. However, the iterator to the first killed node still references a value to a valid pointer to member function. However, incrementing the iterator into the killed list points to garbage, so the second dereference crahes and burns.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!