Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a memory leak in map::generate reported by LSAN in the CI build #36410

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/map_extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2740,14 +2740,13 @@ void apply_function( const string_id<map_extra> &id, map &m, const tripoint &abs
break;
}
case map_extra_method::mapgen: {
tripoint over( abs_sub );
sm_to_omt( over );
mapgendata dat( over, m, 0.0f, calendar::turn, nullptr );
mapgendata dat( sm_to_omt_copy( abs_sub ), m, 0.0f, calendar::turn, nullptr );
run_mapgen_func( extra.generator_id, dat );
break;
}
case map_extra_method::update_mapgen: {
run_mapgen_update_func( extra.generator_id, sm_to_omt_copy( abs_sub ) );
mapgendata dat( sm_to_omt_copy( abs_sub ), m, 0.0f, calendar::start_of_cataclysm, nullptr );
run_mapgen_update_func( extra.generator_id, dat );
break;
}
case map_extra_method::null:
Expand Down
56 changes: 36 additions & 20 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7752,31 +7752,37 @@ bool update_mapgen_function_json::update_map( const tripoint &omt_pos, const poi

mapgendata md( omt_pos, update_tmap, 0.0f, calendar::start_of_cataclysm, miss );

// If the existing map is rotated, we need to rotate it back to the north
// orientation before applying our updates.
const int rotation = oter_get_rotation( overmap_buffer.ter( omt_pos ) );
if( rotation > 0 ) {
md.m.rotate( rotation, true );
}

const bool applied = update_map( md, offset, verify );

// If we rotated the map before applying updates, we now need to rotate
// it back to where we found it.
if( rotation ) {
md.m.rotate( 4 - rotation, true );
}

if( applied ) {
md.m.save();
}

return applied;
return update_map( md, offset, verify );
}

bool update_mapgen_function_json::update_map( mapgendata &md, const point &offset,
const bool verify ) const
{
class rotation_guard
{
public:
rotation_guard( const mapgendata &md )
: md( md ), rotation( oter_get_rotation( md.terrain_type() ) ) {
// If the existing map is rotated, we need to rotate it back to the north
// orientation before applying our updates.
if( rotation != 0 ) {
md.m.rotate( rotation, true );
}
}

~rotation_guard() {
// If we rotated the map before applying updates, we now need to rotate
// it back to where we found it.
if( rotation != 0 ) {
md.m.rotate( 4 - rotation, true );
}
}
private:
const mapgendata &md;
const int rotation;
};
rotation_guard rot( md );

for( auto &elem : setmap_points ) {
if( verify && elem.has_vehicle_collision( md, offset ) ) {
return false;
Expand Down Expand Up @@ -7831,6 +7837,16 @@ bool run_mapgen_update_func( const std::string &update_mapgen_id, const tripoint
return update_function->second[0]->update_map( omt_pos, point_zero, miss, cancel_on_collision );
}

bool run_mapgen_update_func( const std::string &update_mapgen_id, mapgendata &dat,
const bool cancel_on_collision )
{
const auto update_function = update_mapgen.find( update_mapgen_id );
if( update_function == update_mapgen.end() || update_function->second.empty() ) {
return false;
}
return update_function->second[0]->update_map( dat, point_zero, cancel_on_collision );
}

std::pair<std::map<ter_id, int>, std::map<furn_id, int>> get_changed_ids_from_update(
const std::string &update_mapgen_id )
{
Expand Down
2 changes: 2 additions & 0 deletions src/mapgen_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void place_stairs( mapgendata &dat );
mapgen_update_func add_mapgen_update_func( const JsonObject &jo, bool &defer );
bool run_mapgen_update_func( const std::string &update_mapgen_id, const tripoint &omt_pos,
mission *miss = nullptr, bool cancel_on_collision = true );
bool run_mapgen_update_func( const std::string &update_mapgen_id, mapgendata &dat,
bool cancel_on_collision = true );
bool run_mapgen_func( const std::string &mapgen_id, mapgendata &dat );
std::pair<std::map<ter_id, int>, std::map<furn_id, int>> get_changed_ids_from_update(
const std::string &update_mapgen_id );
Expand Down