Segfault when iterating over parameters, but not when using range based loop

How can it be that the first loop works fine, while the second loop gives a segfault. In my understanding, they should be the same.

        for(auto& param : actor->parameters()){
            std::cout<<"Param"<<std::endl;
            std::cout<<param.data()<<std::endl;
        }
        for(auto it = actor->parameters().begin();it!=actor->parameters().end();++it)
        {   
            std::cout<<"Iterator Param"<<std::endl;
            std::cout<<it->data()<<std::endl;
        }

I want to copy parameters between the policy net and the target net, as follows:

         auto it=actor->parameters().begin();
         for(auto target_it = actor_target->parameters().begin();target_it!=actor_target->parameters().end();++target_it){
             target_it->data().copy_(it->data()*tau_+target_it->data()*(1.0-tau_));
             ++it;
         }

For which the python code would be:

# update target networks 
        for target_param, param in zip(self.actor_target.parameters(), self.actor.parameters()):
            target_param.data.copy_(param.data * self.tau + target_param.data * (1.0 - self.tau))
       

I fixed it by doing

        for(int i = 0; i<actor->parameters().size();++i){
            actor_target->parameters()[i].data().copy_(actor->parameters()[i].data()*tau_ + actor_target->parameters()[i].data()*(1.0-tau_));
        }
        

Still wondering why I can’t dereference an iterator on parameters() though.