Crash when snapping verts to grid in vertex mode #1449

Closed
PritchardGSD opened this Issue Sep 28, 2016 · 12 comments

Comments

Projects
None yet
3 participants

lavabloom_quoth-crash-12.zip
lavabloom_quoth-crash-13.zip
Not much to say about this one. Zip file includes a screenshot of TB when I crashed, the vert with the error is pictured.

To reproduce:
Take this brush:
kdbk4s6
Cut it like this:
mlpakum
Extend it with shift-drag like this:
mgx1oko
Vertex manipulate like this:
u7nynl8

Then switch grid size to 1 and "Snap vertices to Grid"

On my system at least this produces a crash.

Owner

kduske commented Sep 28, 2016

I will not be able to reproduce this. Can you please attach the brush to which you apply the snap operation? That way, all I have to do is select it and perform the snap.

crash.zip
This is the best I can do. The map I started to hold /just/ the brushes that I think are affected wouldn't always crash, so you may have trouble. But it did crash once after trying to undo the snap operation, and I included the files that generated.

One issue is that it's not 100% consistent, so sometimes it seems to snap correctly, and it always seems to snap correctly when loading the map from the report so getting the brushes right as they're about to crash seems to be a bit of guesswork.

Owner

kduske commented Oct 2, 2016

Yeah, I need a consistent and reliable reproduction, otherwise I'm just guessing. Maybe @ericwa has an idea?

kduske added this to the TrenchBroom 2.0.0 milestone Oct 2, 2016

Collaborator

ericwa commented Oct 2, 2016

It looks like the crash is just happening when snapping in vertex mode, which I can reproduce.

  1. New map (standard format)
  2. Select the cube
  3. Press v to enter vertex mode
  4. Edit -> Snap vertices to integer, this will crash in VertexHandleManager::removeBrushes
Owner

kduske commented Oct 2, 2016

Excellent, thank you @ericwa. I'll look into it.

Owner

kduske commented Oct 2, 2016

@ericwa unless you want to ;-)

kduske changed the title from Crash when snapping verts to grid to Crash when snapping verts to grid in vertex mode Oct 2, 2016

Collaborator

ericwa commented Oct 2, 2016

@kduske Looks like VertexTool::isVertexCommand is returning true for SnapBrushVerticesCommand, but SnapBrushVerticesCommand is not actually a VertexCommand subclass.
So the static_cast here: https://github.com/kduske/TrenchBroom/blob/release/v2.0.0/common/src/View/VertexTool.cpp#L552 gives a bogus pointer and then things blow up.

Maybe replace:

if (isVertexCommand(command)) {
    VertexCommand* vertexCommand = static_cast<VertexCommand*>(command.get());
    ...

with:

if (VertexCommand* vertexCommand = dynamic_cast<VertexCommand*>(command.get())) {
    ...
Owner

kduske commented Oct 2, 2016

No, I don't want to rely on dynamic_cast. Maybe it should actually be a VertexCommand subclass since it does actually change the vertices. No idea what I was thinking there, to be honest.

Collaborator

ericwa commented Oct 3, 2016

Are you avoiding dynamic_cast altogether in TB? The VertexCommand* vertexCommand = static_cast<VertexCommand*>(command.get()); line is really dangerous with static_cast because you get memory clobbering if there happens to be a bug in isVertexCommand, as in this case.

Owner

kduske commented Oct 3, 2016

Yes, I am. In fact, I'm trying to avoid downcasting altogether. In this instance it's dangerous, yes. But downcasts are very often a design smell, and dynamic casts (and instanceof etc. in Java) just hide the smell a little better so that it's harder to detect.

@kduske kduske added a commit that referenced this issue Oct 3, 2016

@kduske kduske #1449: Don't classify SnapVerticesCommand as a vertex command, since …
…it doesn't inherit from VertexCommand.
2380404
Owner

kduske commented Oct 3, 2016

Fixed in 2380404. Thanks @ericwa for finding the problem.

kduske closed this Oct 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment