Skip to content

Commit b4247df

Browse files
committedApr 1, 2015
Fix usage of destroyed mutex
Also fix a memory leak Fix overloaded virtual warning in Player::move() Remove some trailing whitespace
1 parent 699e066 commit b4247df

File tree

2 files changed

+85
-80
lines changed

2 files changed

+85
-80
lines changed
 

Diff for: ‎src/environment.cpp

+75-73
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ std::vector<Player*> Environment::getPlayers(bool ignore_disconnected)
182182
i = m_players.begin();
183183
i != m_players.end(); ++i) {
184184
Player *player = *i;
185-
185+
186186
if(ignore_disconnected) {
187187
// Ignore disconnected players
188188
if(player->peer_id == 0)
@@ -239,7 +239,7 @@ void Environment::stepTimeOfDay(float dtime)
239239
{
240240
// getTimeOfDaySpeed lock the value we need to prevent MT problems
241241
float day_speed = getTimeOfDaySpeed();
242-
242+
243243
m_time_counter += dtime;
244244
f32 speed = day_speed * 24000./(24.*3600);
245245
u32 units = (u32)(m_time_counter*speed);
@@ -453,41 +453,43 @@ void ServerEnvironment::savePlayer(const std::string &playername)
453453

454454
Player *ServerEnvironment::loadPlayer(const std::string &playername)
455455
{
456-
std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM;
457-
458-
RemotePlayer *player = static_cast<RemotePlayer*>(getPlayer(playername.c_str()));
459456
bool newplayer = false;
460457
bool found = false;
458+
std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM;
459+
std::string path = players_path + playername;
460+
461+
RemotePlayer *player = static_cast<RemotePlayer *>(getPlayer(playername.c_str()));
461462
if (!player) {
462-
player = new RemotePlayer(m_gamedef, playername.c_str());
463+
player = new RemotePlayer(m_gamedef, "");
463464
newplayer = true;
464465
}
465466

466-
RemotePlayer testplayer(m_gamedef, "");
467-
std::string path = players_path + playername;
468467
for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
469-
// Open file and deserialize
468+
//// Open file and deserialize
470469
std::ifstream is(path.c_str(), std::ios_base::binary);
471-
if (!is.good()) {
472-
return NULL;
473-
}
474-
testplayer.deSerialize(is, path);
470+
if (!is.good())
471+
continue;
472+
player->deSerialize(is, path);
475473
is.close();
476-
if (testplayer.getName() == playername) {
477-
*player = testplayer;
474+
475+
if (player->getName() == playername) {
478476
found = true;
479477
break;
480478
}
479+
481480
path = players_path + playername + itos(i);
482481
}
482+
483483
if (!found) {
484484
infostream << "Player file for player " << playername
485485
<< " not found" << std::endl;
486+
if (newplayer)
487+
delete player;
486488
return NULL;
487489
}
488-
if (newplayer) {
490+
491+
if (newplayer)
489492
addPlayer(player);
490-
}
491493
player->setModified(false);
492494
return player;
493495
}
@@ -746,7 +748,7 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime)
746748

747749
/*infostream<<"ServerEnvironment::activateBlock(): block is "
748750
<<dtime_s<<" seconds old."<<std::endl;*/
749-
751+
750752
// Activate stored objects
751753
activateObjects(block, dtime_s);
752754

@@ -990,7 +992,7 @@ void ServerEnvironment::clearAllObjects()
990992
void ServerEnvironment::step(float dtime)
991993
{
992994
DSTACK(__FUNCTION_NAME);
993-
995+
994996
//TimeTaker timer("ServerEnv step");
995997

996998
/* Step time of day */
@@ -1010,7 +1012,7 @@ void ServerEnvironment::step(float dtime)
10101012
m_game_time += inc_i;
10111013
m_game_time_fraction_counter -= (float)inc_i;
10121014
}
1013-
1015+
10141016
/*
10151017
Handle players
10161018
*/
@@ -1020,11 +1022,11 @@ void ServerEnvironment::step(float dtime)
10201022
i != m_players.end(); ++i)
10211023
{
10221024
Player *player = *i;
1023-
1025+
10241026
// Ignore disconnected players
10251027
if(player->peer_id == 0)
10261028
continue;
1027-
1029+
10281030
// Move
10291031
player->move(dtime, this, 100*BS);
10301032
}
@@ -1052,7 +1054,7 @@ void ServerEnvironment::step(float dtime)
10521054
floatToInt(player->getPosition(), BS));
10531055
players_blockpos.push_back(blockpos);
10541056
}
1055-
1057+
10561058
/*
10571059
Update list of active blocks, collecting changes
10581060
*/
@@ -1068,7 +1070,7 @@ void ServerEnvironment::step(float dtime)
10681070

10691071
// Convert active objects that are no more in active blocks to static
10701072
deactivateFarObjects(false);
1071-
1073+
10721074
for(std::set<v3s16>::iterator
10731075
i = blocks_removed.begin();
10741076
i != blocks_removed.end(); ++i)
@@ -1077,11 +1079,11 @@ void ServerEnvironment::step(float dtime)
10771079

10781080
/* infostream<<"Server: Block " << PP(p)
10791081
<< " became inactive"<<std::endl; */
1080-
1082+
10811083
MapBlock *block = m_map->getBlockNoCreateNoEx(p);
10821084
if(block==NULL)
10831085
continue;
1084-
1086+
10851087
// Set current time as timestamp (and let it set ChangedFlag)
10861088
block->setTimestamp(m_game_time);
10871089
}
@@ -1114,15 +1116,15 @@ void ServerEnvironment::step(float dtime)
11141116
if(m_active_blocks_nodemetadata_interval.step(dtime, 1.0))
11151117
{
11161118
ScopeProfiler sp(g_profiler, "SEnv: mess in act. blocks avg /1s", SPT_AVG);
1117-
1119+
11181120
float dtime = 1.0;
11191121

11201122
for(std::set<v3s16>::iterator
11211123
i = m_active_blocks.m_list.begin();
11221124
i != m_active_blocks.m_list.end(); ++i)
11231125
{
11241126
v3s16 p = *i;
1125-
1127+
11261128
/*infostream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
11271129
<<") being handled"<<std::endl;*/
11281130

@@ -1132,7 +1134,7 @@ void ServerEnvironment::step(float dtime)
11321134

11331135
// Reset block usage timer
11341136
block->resetUsageTimer();
1135-
1137+
11361138
// Set current time as timestamp
11371139
block->setTimestampNoChangedFlag(m_game_time);
11381140
// If time has changed much from the one on disk,
@@ -1157,7 +1159,7 @@ void ServerEnvironment::step(float dtime)
11571159
}
11581160
}
11591161
}
1160-
1162+
11611163
const float abm_interval = 1.0;
11621164
if(m_active_block_modifier_interval.step(dtime, abm_interval))
11631165
do{ // breakable
@@ -1168,7 +1170,7 @@ void ServerEnvironment::step(float dtime)
11681170
}
11691171
ScopeProfiler sp(g_profiler, "SEnv: modify in blocks avg /1s", SPT_AVG);
11701172
TimeTaker timer("modify in active blocks");
1171-
1173+
11721174
// Initialize handling of ActiveBlockModifiers
11731175
ABMHandler abmhandler(m_abms, abm_interval, this, true);
11741176

@@ -1177,14 +1179,14 @@ void ServerEnvironment::step(float dtime)
11771179
i != m_active_blocks.m_list.end(); ++i)
11781180
{
11791181
v3s16 p = *i;
1180-
1182+
11811183
/*infostream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
11821184
<<") being handled"<<std::endl;*/
11831185

11841186
MapBlock *block = m_map->getBlockNoCreateNoEx(p);
11851187
if(block == NULL)
11861188
continue;
1187-
1189+
11881190
// Set current time as timestamp
11891191
block->setTimestampNoChangedFlag(m_game_time);
11901192

@@ -1201,7 +1203,7 @@ void ServerEnvironment::step(float dtime)
12011203
m_active_block_interval_overload_skip = (time_ms / max_time_ms) + 1;
12021204
}
12031205
}while(0);
1204-
1206+
12051207
/*
12061208
Step script environment (run global on_step())
12071209
*/
@@ -1215,7 +1217,7 @@ void ServerEnvironment::step(float dtime)
12151217
//TimeTaker timer("Step active objects");
12161218

12171219
g_profiler->avg("SEnv: num of objects", m_active_objects.size());
1218-
1220+
12191221
// This helps the objects to send data at the same time
12201222
bool send_recommended = false;
12211223
m_send_recommended_timer += dtime;
@@ -1244,7 +1246,7 @@ void ServerEnvironment::step(float dtime)
12441246
}
12451247
}
12461248
}
1247-
1249+
12481250
/*
12491251
Manage active objects
12501252
*/
@@ -1287,7 +1289,7 @@ u16 getFreeServerActiveObjectId(
12871289
last_used_id ++;
12881290
if(isFreeServerActiveObjectId(last_used_id, objects))
12891291
return last_used_id;
1290-
1292+
12911293
if(last_used_id == startid)
12921294
return 0;
12931295
}
@@ -1443,7 +1445,7 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
14431445
removed_objects.insert(id);
14441446
continue;
14451447
}
1446-
1448+
14471449
f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
14481450
if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
14491451
if (distance_f <= player_radius_f || player_radius_f == 0)
@@ -1460,7 +1462,7 @@ ActiveObjectMessage ServerEnvironment::getActiveObjectMessage()
14601462
{
14611463
if(m_active_object_messages.empty())
14621464
return ActiveObjectMessage(0);
1463-
1465+
14641466
ActiveObjectMessage message = m_active_object_messages.front();
14651467
m_active_object_messages.pop_front();
14661468
return message;
@@ -1500,19 +1502,19 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
15001502
}
15011503
/*infostream<<"ServerEnvironment::addActiveObjectRaw(): "
15021504
<<"added (id="<<object->getId()<<")"<<std::endl;*/
1503-
1505+
15041506
m_active_objects[object->getId()] = object;
1505-
1507+
15061508
verbosestream<<"ServerEnvironment::addActiveObjectRaw(): "
15071509
<<"Added id="<<object->getId()<<"; there are now "
15081510
<<m_active_objects.size()<<" active objects."
15091511
<<std::endl;
1510-
1512+
15111513
// Register reference in scripting api (must be done before post-init)
15121514
m_script->addObjectReference(object);
15131515
// Post-initialize object
15141516
object->addedToEnvironment(dtime_s);
1515-
1517+
15161518
// Add static data to block
15171519
if(object->isStaticAllowed())
15181520
{
@@ -1538,7 +1540,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
15381540
<<" statically (pos="<<PP(p)<<")"<<std::endl;
15391541
}
15401542
}
1541-
1543+
15421544
return object->getId();
15431545
}
15441546

@@ -1712,7 +1714,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
17121714
<<"in block "<<PP(s_obj.pos/BS)
17131715
<<" type="<<(int)s_obj.type<<" data:"<<std::endl;
17141716
print_hexdump(verbosestream, s_obj.data);
1715-
1717+
17161718
new_stored.push_back(s_obj);
17171719
continue;
17181720
}
@@ -1773,7 +1775,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
17731775
i != m_active_objects.end(); ++i) {
17741776
ServerActiveObject* obj = i->second;
17751777
assert(obj);
1776-
1778+
17771779
// Do not deactivate if static data creation not allowed
17781780
if(!force_delete && !obj->isStaticAllowed())
17791781
continue;
@@ -1849,7 +1851,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
18491851
// Create new static object
18501852
std::string staticdata_new = obj->getStaticData();
18511853
StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
1852-
1854+
18531855
bool stays_in_same_block = false;
18541856
bool data_changed = true;
18551857

@@ -1858,7 +1860,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
18581860
stays_in_same_block = true;
18591861

18601862
MapBlock *block = m_map->emergeBlock(obj->m_static_block, false);
1861-
1863+
18621864
std::map<u16, StaticObject>::iterator n =
18631865
block->m_static_objects.m_active.find(id);
18641866
if(n != block->m_static_objects.m_active.end()){
@@ -1878,7 +1880,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
18781880
}
18791881

18801882
bool shall_be_written = (!stays_in_same_block || data_changed);
1881-
1883+
18821884
// Delete old static object
18831885
if(obj->m_static_exists)
18841886
{
@@ -1933,13 +1935,13 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
19331935
// Store static data
19341936
u16 store_id = pending_delete ? id : 0;
19351937
block->m_static_objects.insert(store_id, s_obj);
1936-
1938+
19371939
// Only mark block as modified if data changed considerably
19381940
if(shall_be_written)
19391941
block->raiseModified(MOD_STATE_WRITE_NEEDED,
19401942
"deactivateFarObjects: Static data "
19411943
"changed considerably");
1942-
1944+
19431945
obj->m_static_exists = true;
19441946
obj->m_static_block = block->getPos();
19451947
}
@@ -1969,7 +1971,7 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
19691971
obj->m_pending_deactivation = true;
19701972
continue;
19711973
}
1972-
1974+
19731975
verbosestream<<"ServerEnvironment::deactivateFarObjects(): "
19741976
<<"object id="<<id<<" is not known by clients"
19751977
<<"; deleting"<<std::endl;
@@ -2084,14 +2086,14 @@ void ClientEnvironment::step(float dtime)
20842086
assert(lplayer);
20852087
// collision info queue
20862088
std::vector<CollisionInfo> player_collisions;
2087-
2089+
20882090
/*
20892091
Get the speed the player is going
20902092
*/
20912093
bool is_climbing = lplayer->is_climbing;
2092-
2094+
20932095
f32 player_speed = lplayer->getSpeed().getLength();
2094-
2096+
20952097
/*
20962098
Maximum position increment
20972099
*/
@@ -2103,15 +2105,15 @@ void ClientEnvironment::step(float dtime)
21032105
f32 dtime_max_increment = 1;
21042106
if(player_speed > 0.001)
21052107
dtime_max_increment = position_max_increment / player_speed;
2106-
2108+
21072109
// Maximum time increment is 10ms or lower
21082110
if(dtime_max_increment > 0.01)
21092111
dtime_max_increment = 0.01;
2110-
2112+
21112113
// Don't allow overly huge dtime
21122114
if(dtime > 0.5)
21132115
dtime = 0.5;
2114-
2116+
21152117
f32 dtime_downcount = dtime;
21162118

21172119
/*
@@ -2139,11 +2141,11 @@ void ClientEnvironment::step(float dtime)
21392141
*/
21402142
dtime_downcount = 0;
21412143
}
2142-
2144+
21432145
/*
21442146
Handle local player
21452147
*/
2146-
2148+
21472149
{
21482150
// Apply physics
21492151
if(free_move == false && is_climbing == false)
@@ -2169,10 +2171,10 @@ void ClientEnvironment::step(float dtime)
21692171
if(dl > lplayer->movement_liquid_fluidity_smooth)
21702172
dl = lplayer->movement_liquid_fluidity_smooth;
21712173
dl *= (lplayer->liquid_viscosity * viscosity_factor) + (1 - viscosity_factor);
2172-
2174+
21732175
v3f d = d_wanted.normalize() * dl;
21742176
speed += d;
2175-
2177+
21762178
#if 0 // old code
21772179
if(speed.X > lplayer->movement_liquid_fluidity + lplayer->movement_liquid_fluidity_smooth) speed.X -= lplayer->movement_liquid_fluidity_smooth;
21782180
if(speed.X < -lplayer->movement_liquid_fluidity - lplayer->movement_liquid_fluidity_smooth) speed.X += lplayer->movement_liquid_fluidity_smooth;
@@ -2195,9 +2197,9 @@ void ClientEnvironment::step(float dtime)
21952197
}
21962198
}
21972199
while(dtime_downcount > 0.001);
2198-
2200+
21992201
//std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
2200-
2202+
22012203
for(std::vector<CollisionInfo>::iterator i = player_collisions.begin();
22022204
i != player_collisions.end(); ++i) {
22032205
CollisionInfo &info = *i;
@@ -2232,14 +2234,14 @@ void ClientEnvironment::step(float dtime)
22322234
}
22332235
}
22342236
}
2235-
2237+
22362238
/*
22372239
A quick draft of lava damage
22382240
*/
22392241
if(m_lava_hurt_interval.step(dtime, 1.0))
22402242
{
22412243
v3f pf = lplayer->getPosition();
2242-
2244+
22432245
// Feet, middle and head
22442246
v3s16 p1 = floatToInt(pf + v3f(0, BS*0.1, 0), BS);
22452247
MapNode n1 = m_map->getNodeNoEx(p1);
@@ -2255,7 +2257,7 @@ void ClientEnvironment::step(float dtime)
22552257
m_gamedef->ndef()->get(n2).damage_per_second);
22562258
damage_per_second = MYMAX(damage_per_second,
22572259
m_gamedef->ndef()->get(n3).damage_per_second);
2258-
2260+
22592261
if(damage_per_second != 0)
22602262
{
22612263
damageLocalPlayer(damage_per_second, true);
@@ -2317,7 +2319,7 @@ void ClientEnvironment::step(float dtime)
23172319
for(std::vector<Player*>::iterator i = m_players.begin();
23182320
i != m_players.end(); ++i) {
23192321
Player *player = *i;
2320-
2322+
23212323
/*
23222324
Handle non-local players
23232325
*/
@@ -2349,7 +2351,7 @@ void ClientEnvironment::step(float dtime)
23492351
/*
23502352
Step active objects and update lighting of them
23512353
*/
2352-
2354+
23532355
g_profiler->avg("CEnv: num of objects", m_active_objects.size());
23542356
bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
23552357
for(std::map<u16, ClientActiveObject*>::iterator
@@ -2397,7 +2399,7 @@ void ClientEnvironment::step(float dtime)
23972399
}
23982400
}
23992401
}
2400-
2402+
24012403
void ClientEnvironment::addSimpleObject(ClientSimpleObject *simple)
24022404
{
24032405
m_simple_objects.push_back(simple);
@@ -2432,7 +2434,7 @@ u16 getFreeClientActiveObjectId(
24322434
last_used_id ++;
24332435
if(isFreeClientActiveObjectId(last_used_id, objects))
24342436
return last_used_id;
2435-
2437+
24362438
if(last_used_id == startid)
24372439
return 0;
24382440
}
@@ -2493,7 +2495,7 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type,
24932495
<<std::endl;
24942496
return;
24952497
}
2496-
2498+
24972499
obj->setId(id);
24982500

24992501
try
@@ -2588,7 +2590,7 @@ void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
25882590
/*
25892591
Client likes to call these
25902592
*/
2591-
2593+
25922594
void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
25932595
std::vector<DistanceSortedActiveObject> &dest)
25942596
{

Diff for: ‎src/player.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class PlayerSAO;
9292
struct HudElement;
9393
class Environment;
9494

95+
// IMPORTANT:
96+
// Do *not* perform an assignment or copy operation on a Player or
97+
// RemotePlayer object! This will copy the lock held for HUD synchronization
9598
class Player
9699
{
97100
public:
@@ -102,7 +105,7 @@ class Player
102105
virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
103106
{}
104107
virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
105-
std::list<CollisionInfo> *collision_info)
108+
std::vector<CollisionInfo> *collision_info)
106109
{}
107110

108111
v3f getSpeed()
@@ -114,7 +117,7 @@ class Player
114117
{
115118
m_speed = speed;
116119
}
117-
120+
118121
void accelerateHorizontal(v3f target_speed, f32 max_increase);
119122
void accelerateVertical(v3f target_speed, f32 max_increase);
120123

@@ -252,7 +255,7 @@ class Player
252255
bool is_climbing;
253256
bool swimming_vertical;
254257
bool camera_barely_in_ceiling;
255-
258+
256259
Inventory inventory;
257260

258261
f32 movement_acceleration_default;
@@ -285,15 +288,15 @@ class Player
285288
u16 peer_id;
286289

287290
std::string inventory_formspec;
288-
291+
289292
PlayerControl control;
290293
PlayerControl getPlayerControl()
291294
{
292295
return control;
293296
}
294-
297+
295298
u32 keyPressed;
296-
299+
297300

298301
HudElement* getHud(u32 id);
299302
u32 addHud(HudElement* hud);
@@ -346,7 +349,7 @@ class RemotePlayer : public Player
346349
void setPlayerSAO(PlayerSAO *sao)
347350
{ m_sao = sao; }
348351
void setPosition(const v3f &position);
349-
352+
350353
private:
351354
PlayerSAO *m_sao;
352355
};

0 commit comments

Comments
 (0)
Please sign in to comment.