[英]segmentation fault in iterator
I have created a multimap for my road points. 我为路标创建了一个多图。 The key refers to the road number and the values are vec3 points that make up the road.
关键是指道路编号,其值是构成道路的vec3点。
I am trying to iterate through the values of each key point and create a road segment at each point on the road (except the last), adjust the values to be on the road points and then store them in a std::vector. 我正在尝试遍历每个关键点的值,并在道路上的每个点(最后一个除外)上创建路段,将值调整为在道路点上,然后将其存储在std :: vector中。
The RoadSegment constructor creates 6 vec3 points and pushes them onto a std::vector. RoadSegment构造函数创建6个vec3点,并将其推入std :: vector。
I have a segmentation fault in the line marked in bold [for(mapIt = it.first; mapIt != it.second; ++mapIt)] 我在以粗体标出的行中存在细分错误[for(mapIt = it.first; mapIt!= it.second; ++ mapIt)]
When i take out the lines creating the new objects and pushing them onto the std::vector it works fine. 当我取出创建新对象的行并将其推到std :: vector上时,它工作正常。
Can anyone tell me what the problem is / a solution to the problem?? 谁能告诉我问题是什么/问题的解决方案?
Many thanks in advance 提前谢谢了
std::vector<glm::vec3>::iterator SegIt;
for(int i = 0; i < m_genRoads->getKeyValueData().size(); i++)
{
int numberDesired = m_genRoads->getMultimapData().count(i) - 1;
std::multimap<int, glm::vec3>::iterator mapIt;
std::pair<std::multimap<int, glm::vec3>::iterator, std::multimap<int, glm::vec3>::iterator> it;
it = m_genRoads->getMultimapData().equal_range(i);
for(mapIt = it.first; mapIt != it.second; ++mapIt)
{
int distance = std::distance(it.first, mapIt);
if(distance != numberDesired)
{
RoadSegement* roadSegmentPointer = new RoadSegement();
// FUNCTIONS TO ADJUST COORD VALUES TO MATCH THE ROAD POINTS
m_segmentArray.push_back(roadSegmentPointer);
}
else
{
continue;
}
///SOME BUFFER BINDING STUFF
The issue seems to be that you're using iterators that do not exist, all due to returning a temporary object. 问题似乎是您使用的迭代器不存在,这都是由于返回了一个临时对象。
it = m_genRoads->getMultimapData().equal_range(i);
Since getMultiMapData()
returns a copy of the multimap, that multimap is gone after the line is executed, thus rendering any iterators invalid. 由于
getMultiMapData()
返回多图的副本,因此该多图在执行该行后就消失了,从而使所有迭代器均无效。
One solution is to return a reference to the multimap, not a new copy of the multimap. 一种解决方案是返回对多图的引用,而不是多图的新副本。
std::multimap<int, glm::vec3>& GenerateRoads::getMultimapData() { return m_roadsMultimap; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.