last day (15 days later) » 

8:55 PM
-1
Q: New Elements Sometimes Don't get pushed into Map while they do in a Vector

AndreaM16I'm having a pretty difficult time with inserting new elements into a certain map in C++. Before anyone asks The geometry types that you see are accessible ONLY through pointers. I'm working on a 3D Convex Hull Solver for a University Exam. I'm a C++ Beginner. The map is a map of poin...

 
Suppose I have 2 Halfedges given by v1,v2 and v2,v3, If i store them in a map this problem that you are saying may happen?
I never use the same vertex as key. I updated the question.
@ThomasMatthews The problem occurs also using unordered_map. I'm a C++ beginner so, if you have a solution to this issue, please share it. Thanks. As I said earlier, I can access vertices only using a pointer to them, can't do anything about that.
I can't. I can access this particular type ONLY using pointers. Same thing for halfEdges, faces etc. I would love to not use them, but, unluckily, I have to work with these types.
 
@AndreaM16 -- Using pointers as keys in a map has all sorts of dangers. First, what if somewhere else in the code, that pointer becomes invalidated (i.e., is "freed", goes out of scope, etc.)? You now have a map with a bogus key inside of it. What happens if the pointer gets reseated to another object? You have a valid key, but the data that the key represents is not correct. -- Suppose we have 3 halfEdges: -- Your example of what you expect to happen basically shows us why using pointers is difficult to maintain -- your own explanation does not use pointers, but uses objects.
 
@PaulMcKenzie you are absolutely right And I know that. But, as I stated multiple times, I really can access these particular types only using pointers.
 
@AndreaM16 -- Did you investigate the reasons I pointed out as to why there can be issues with pointers? How do you know you're not running into those issues I pointed out? Either that, or you're using the same pointer value once, then using it again to point to another vertex, and the second insert doesn't "work". I suggest you call map::insert() instead of using [ ] to add entries to the map, and check the return value for insert(). That function returns a std::pair. Check the second of that pair. If it's "false", it means that the item already existed.
 
@PaulMcKenzie yes, I did for the past week. I'm not 100% I'm not running in these issues because I'm a beginner, because the application is huge and complex, but I'm 100% sure, that, debugging, all the vertices are not corrupted, that they get correctly inserted in both map and vector and I'm also sure that I'm fighting with this bug from weeks.
 
8:55 PM
@AndreaM16 -- See my updated comment. Use insert() and check the return value. If the map::insert(make_pair(vertex*, vertex*)) returns a pair where the second is false, it means that the item already existed and the insert failed. That right there is where you should start your debugging.
 
@PaulMcKenzie I'm giving it a shot. I read that the recommended way to set a new element is [] that's why I'm using it.
 
@AndreaM16 auto pr = mapOfVertices.insert(make_pair(outerHalfEdgeTwinFrom, outerHalfEdgeTwinTo)); if (!pr.second) { insert failed }. The test should look something like that.
 
Hi, Thanks for you r precious time Paul
I tried, It's happening what you said
 
Basically you never tested to see if the insertion into the map passed or failed, and the easiest way to detect it is to use insert(), not [ ]
 
I'm a beginner, didn't really think about that.
Sorry
 
8:58 PM
So you have to fix the bug as to why the pointer already existed, and you're attempting to add it again to the map.
 
Any suggestion? As I said, I can use these particular types only through pointers
Also the tutor hasn't found a solution to that. I'm struggling with this from weeks.
 
That is a bug in the logic. All I can tell you is the reason why your map didn't become populated is that there is a pointer with that same values as a key already existing. You have to use the debugger to figure out how it got there originally, and why it's still there at the point where the insert failed.
Probably you forgot to update the map when the pointer became invalidated at some point? Remember that you are trying to keep in sync the vector and the map, and somewhere it got out of sync?
 
They do not go out of sync, with the debugger, step by step, I just see one of these insertions skipped without any reason.
I have like 20k Vertices
and this happens ~2 out of 10 times
not all the times
that's what's making me mad
If only one of this insertions does not happens, the application crashes since I can't draw a valid mesh
I send you a small screenshot
 
Why so much data? Why not try a smaller data set first? And the reason as I stated is that you're inserting a pointer that was placed in the map before, and you're trying to insert the same pointer value again. It's that cut and dry.
 
I can't decide, It's a university project
they give us some 3d models
and pre builded methods to work with them
then you have to find a way to build a 3d convex hull of the object
I have 10 mb models for instance
 
9:05 PM
You're using a recycled pointer believing it is a unique pointer to a unique vertex. Not so.
 
Here, as you can see, the last halfEdge's from and toVertex don;t get inserted
 
To make it even more convincing, I bet your vector has duplicates in it.
 
You can see identical memory locations fro the different points which are the same for each fromVertex and toVertex except the last one
 
Change the vector to a std::set() and call insert() instead of push_back. That will convince you that what you were putting into the vector were duplicate keys.
 
You are right
 
9:09 PM
Don't know what that picture shows me, but like I said, you were sticking duplicates in your vector and you never realized it. A vector doesn't check for duplicates, a std::set does.
 
2 and 3 elemtns in the vector have same from vertex
location
I don't really get it, how is this happening?
 
Look at the source of where those pointers are coming from.
outerHalfEdgeTwin = currHalfEdge->getTwin();
...
outerHalfEdgeTwinFrom = outerHalfEdgeTwin->getFromVertex();
outerHalfEdgeTwinTo = outerHalfEdgeTwin->getToVertex();
 
Okay I'll investigate on that. Thanks in advance. C++ Community on SO is pretty tough, maybe I had a wrong approach. I'll let you know and, If you want to post an answer to just clarify that for other users you are welcome.
 
You were looking in the wrong place (the vector and map -- these only revealed the problem to you). You should really be checking those get functions
 
Yes thanks. The project is huge and I was sure that is was caused by the map. I was clearly wrong.
 

last day (15 days later) »