Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Segmentation Fault

Comments in 'Plugin Development' started by Samueljh1H8sLag, Jul 20, 2015.

  1. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    For some reason when I run an asynctask and inside that, perform a call back, I get a segmentation fault.

    Here is the main class code:

    PHP:
    $sender->sendMessage("§4§lLoading Stats§8...");
    $this->getServer()->getScheduler()->scheduleAsyncTask(new showPlayerData($this->pluginserialize($sender), $sender->getName()));
    I also tried without serialising the sender var.

    showPlayerData.php

    PHP:

    <?php

    #### SamCraft SkyWars - Core Plugin | LB Task ###

    namespace Samueljh1\Samcraft\SkyWars\LeaderboardSystem;

    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\scheduler\ServerScheduler;
    use 
    pocketmine\scheduler\AsyncTask;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Server;

    class 
    showPlayerData extends AsyncTask{

               public 
    $plugin$sender$name;

               public function 
    __construct($plugin$sender$name){

                      
    $this->plugin $plugin;
                      
    $this->sender $sender;
                      
    $this->name $name;

               }

               public function 
    onRun(){

                          
    $this->data explode(","file_get_contents("http://skywars.samueljh1.net/LeaderboardRequest.php?usr=" $this->name));

               }

               public function 
    onCompletion(Server $server){

                           
    $this->plugin->sendStatsMessage($this->sender$this->data$this->name);

               }

    }

    And the sendStatsMessage() function:

    PHP:
    public function sendStatsMessage($sender$data$name){

               
    $sender unserialize($sender);

               if(
    $data[0] !== "Error") {

                          
    $this->setPlayerData($data);

                          if (isset(
    $data[5])) {

                                     
    $pname $data[0];
                                     
    $score $data[1];
                                     
    $kills $data[2];
                                     
    $deaths $data[3];
                                     
    $rank str_replace("Plus""+"$data[5]);

                                     
    $this->showStatMessage($sender$pname$score$kills$deaths$rank);

                          }

                          else {

                                     
    $this->showStatMessage($sender$data[0], 000"Member");

                          }

               }

               else{

                          
    $sender->sendMessage("§8§l\"§6" $name "§8\" §4is not a Player!");

               }

    }
    Everything works, the message sends with the correct data - but the segmentation fault occurs after the sendStatsMessage() function finishes.

    Any Ideas? :)
  2. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
  3. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    You're programming SkyWars too? Good luck ;)
  4. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    My experience have been that serializing $sender (specially if it is a Player) is problematic. The way I got around it is (again assuming it is player), is I only pass $sender->getName() to AsyncTask. Then to get the Sender later from the onCompletion, I do $sender = $server->getPlayer($sender_name). Make sure you check for null returns.

    It is probably good to do this anyway because the Player could have logged out by the time the AsyncTask finishes.
    Falk and TryoneLegendZzz like this.
  5. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    Oh yea forgot about task ending and player quitting xD

    Also @Kvetinac97 i have already made the plugin xD (sw.samueljh1.net:19133) :p
  6. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    This is because the Player is connected to the Server object, for example $player->getLevel()->getServer(), and serializing the entire server will eat your memory very quickly.
    PEMapModder likes this.

Share This Page

Advertisement