Page 1 of 1

Gotcha: Segmentation Fault in Trigger callback

Posted: Tue Apr 23, 2013 2:06 pm
by rumex
FYI: I spent a frustrating while realising that deleting a timer from within code called by that timer triggering was a bad idea.
The solution was to use distimer instead.

[this is a summary of a thread on the old nabble forum]

Re: Gotcha: Segmentation Fault in Trigger callback

Posted: Tue Apr 23, 2013 3:41 pm
by jmud
If you really want to delete the timer, you could add it to a global list:

Code: Select all

# Create a global variable
our @DeleteTimerList;

# Create a KildClient timer
$::world->timer(
            {
                name => $timerName,
                interval => $timerInterval,
                action => $timerAction,
            }
);

# When you want to delete the timer, add it to death row
push (@DeleteTimerList, $timerName);
Also, you can create a second timer that calls a function in your plugin, that empties @DeleteTimerList periodically.

Code: Select all

$::world->timer(
            {
                name => 'Grim_reaper',
                interval => 1,
                action => '/myplugin->deleteMyTimers(),
            }
);

...

sub deleteMyTimer {

	foreach my $timerName (@DeleteTimerList) {
		
		$result = $::world->deltimer($timerName);
	}
}
This should guarantee that you never delete the timer, from within the Perl code it calls. (It's probably simpler to use distimer, as you say.)