Server crash question

For people working on the C++ code.
Post Reply
User avatar
joe7575
Member
Posts: 851
Joined: Mon Apr 24, 2017 20:38
GitHub: joe7575
In-game: JoSto wuffi
Location: Germany, in the deep south

Server crash question

by joe7575 » Post

This small script leads to a server crash when the player leaves the game.

Code: Select all

local function jail_player(pos, player)
	if pos and player then
		player:setpos(pos)	
		minetest.after(1, jail_player, pos, player)
	end
end

minetest.register_on_joinplayer(function(player)
	local pos = player:getpos()
	jail_player(pos, player)
end)
The player object is still valid, but the player is already gone. A call to player:setpos(pos) leads to a memory access error:

Code: Select all

2018-04-13 19:23:22: ACTION[Server]: JoSto leaves game. List of players: 
Speicherzugriffsfehler (Speicherabzug geschrieben)
I use: Minetest 0.4.16 (Linux)

Is this a core issue or a modding fault?
Sent from my Commodore 64. Some of my Mods: Tech Age, TechPack, Hyperloop, Tower Crane, Lumberjack, vm16, Minecart, Signs Bot.

User avatar
Krock
Developer
Posts: 4649
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: Server crash question

by Krock » Post

Modding fault. Never use an ObjectRef outside the scope of the callback function. Instead of passing the player object to your "jail_player" function, use the player name and run "minetest.get_player_name(name)" to check whether the player is still online.

Hint: If you attach the player to an invisible, indestructible object on join and occasionally after each player's death, the player can't move at all.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Server crash question

by rubenwardy » Post

This is a classic example of the biggest common mistake: https://rubenwardy.com/minetest_modding ... r-entities
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
joe7575
Member
Posts: 851
Joined: Mon Apr 24, 2017 20:38
GitHub: joe7575
In-game: JoSto wuffi
Location: Germany, in the deep south

Re: Server crash question

by joe7575 » Post

Thanks, I got it. (...RTFM)
@Knock: This was only a simple example to explain the problem, not a real use case.
Sent from my Commodore 64. Some of my Mods: Tech Age, TechPack, Hyperloop, Tower Crane, Lumberjack, vm16, Minecart, Signs Bot.

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Server crash question

by rubenwardy » Post

Another thing: it's set_pos not setpos. The method has been renamed (since like 0.4.14 or smth) :)
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests