From 6cda5fe31dffd0c018008e0e5d8921f9c0cc8a43 Mon Sep 17 00:00:00 2001 From: Matthew Cline Date: Sat, 14 Nov 2009 22:21:58 -0800 Subject: teleport.cc: hack to deal with memory bug random_space_weighted() sometimes gets this weird memory bug which causes the return value to be random (and usually out-of-bounds). "Fixed" by testing the result against in_bounds() --- crawl-ref/source/teleport.cc | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'crawl-ref/source/teleport.cc') diff --git a/crawl-ref/source/teleport.cc b/crawl-ref/source/teleport.cc index 8b96ed6b35..aafc685583 100644 --- a/crawl-ref/source/teleport.cc +++ b/crawl-ref/source/teleport.cc @@ -68,6 +68,9 @@ typedef std::pair coord_weight; // Try to find a "safe" place for moved close or far from the target. // keep_los indicates that the destination should be in view of the target. +// +// XXX: Check the result against in_bounds(), not coord_def::origin(), +// beceause of a memory problem described below. static coord_def random_space_weighted(actor* moved, actor* target, bool close, bool keep_los = true, bool allow_sanct = true) @@ -95,6 +98,13 @@ static coord_def random_space_weighted(actor* moved, actor* target, weight = 0; dests.push_back(coord_weight(*ri, weight)); } + + // XXX: Sometimes this results in a memory problem, where it seems as + // if the dests vector is free'd before copying the result. When + // this happens, an out-of-bounds coordinate is usually returned. + // + // This is with GCC 4.4.1, -O2, 32 bit Linux, -march-native on a + // Family 10 AMD chip. coord_def* choice = random_choose_weighted(dests); return (choice ? *choice : coord_def(0, 0)); } @@ -108,7 +118,7 @@ void blink_other_close(actor* victim, const coord_def &target) if (is_sanctuary(you.pos())) return; coord_def dest = random_space_weighted(victim, caster, true); - if (dest.origin()) + if (!in_bounds(dest)) return; bool success = victim->blink_to(dest); ASSERT(success); @@ -121,7 +131,7 @@ void blink_away(monsters* mon) if (!foe || !mon->can_see(foe)) return; coord_def dest = random_space_weighted(mon, foe, false, false); - if (dest.origin()) + if (!in_bounds(dest)) return; bool success = mon->blink_to(dest); ASSERT(success); @@ -134,7 +144,7 @@ void blink_range(monsters* mon) if (!foe || !mon->can_see(foe)) return; coord_def dest = random_space_weighted(mon, foe, false, true); - if (dest.origin()) + if (!in_bounds(dest)) return; bool success = mon->blink_to(dest); ASSERT(success); @@ -147,7 +157,7 @@ void blink_close(monsters* mon) if (!foe || !mon->can_see(foe)) return; coord_def dest = random_space_weighted(mon, foe, true); - if (dest.origin()) + if (!in_bounds(dest)) return; bool success = mon->blink_to(dest); ASSERT(success); -- cgit v1.2.3-54-g00ecf