1. I probably found the bug.
    Here's code snippet:
    http://pastebin.com/0G1xSYCp

    Result: Timer is lost, and there is no way to stop it unless you saved its handle during creation.

    Bug or plugin design mistake?
     
  2. Wulf

    Wulf Community Admin

    I'm not quite sure what you're referring to. Timers are destroyed on Unload by default, as long as the plugin instance is passed to it.
     
    Last edited: Jul 30, 2015
  3. always been like that.
    you need to destroy the timer on plugin unload.
    [DOUBLEPOST=1438291934][/DOUBLEPOST]
    is it now? because before we always had to manually unlaod the timers
     
  4. Wulf

    Wulf Community Admin

    It's always been automatic, a lot of people just didn't pass the plugin instance/owner to it. ;)
     
  5. Okay. Let me show again :)
    http://pastebin.com/YbHWBeYG

    Result:
    It will happen only if you reload plugin after the server has already been loaded.
     
  6. Wulf

    Wulf Community Admin

    Yeah, that's because you haven't destroyed it and you didn't pass the plugin instance to it, so the server has no idea what plugin owns the timer.
     
  7. So.. this can be considered as a plugin design error?
    I am just a bit frustrated because OnServerInitialized is still being called, even if plugin was Unloaded and thus timer is created after unloading and it wont be destroyed automatically.

    Well, my mistake, probably. Sorry for bothering.
     
  8. Wulf

    Wulf Community Admin

    I'm not sure how to do it with C# plugins, but with all the others, simply passing the plugin instance at the end of the timer is the solution.
    Code:
    timer.Repeat(10, 0, function()
        rust.BroadcastChat("SERVER", "Hello world!")
    end, self.Plugin)
     
  9. But it's happening already i suppose?

    PluginTimers.cs
    Code:
            public Timer Every(float interval, Action callback)
            {
                return new Timer(timer.Repeat(interval, -1, callback, plugin));
            }
    RustPlugin.cs / CSharpPlugin.cs
    Code:
            public abstract class RustPlugin : CSharpPlugin        ....        public CSharpPlugin() : base()
            {
                timer = new PluginTimers(this);
    To make sure, i've created timer in a explicit way (as you did):
    http://pastebin.com/uP41FaWQ
    And nothing changed.
    The likely problem is that timer is being created after when life cycle of plugin has ended. Not because the timer is doesn't know who created him.

    Such behavior seems too weird. OnServerInitialized shouldn't be called if plugin is already unloaded, either timers shouldn't be created under same circumstances.

    p.s Whoops. Please, move this post to the new thread too :)
     
    Last edited by a moderator: Jul 30, 2015
  10. Wulf

    Wulf Community Admin

    I'll pass it by the others, as they're the C# guys. :p
     
  11. Don't be lazy and rely on others to clean up your timers, do it properly like this:

    Code:
    namespace Oxide.Plugins
    {
        [Info("TEST", "TEST by Deicide666ra", "1.0.0")]
        public class Test : RustPlugin
        {
            private Timer g_timer;        void OnServerInitialized()
            {
                g_timer = timer.Every(300, OnTimer);
            }        void OnTimer()
            {
                Puts("Tick tock!");
            }        void Unloaded()
            {
                if (g_timer != null)
                {
                    g_timer.Destroy();
                    g_timer = null;
                }
            }
        }
    }
     
  12. Don't be lazy reading the code a bit more carefully.
    This happens when plugin unloads itself, and of course i taken care about the timer destroy in Unloaded method(which isn't necessary!), if you didn't seen it too.
     
  13. I see, sorry I only read the first snippet which did not destroy the timer.

    Why not use OnLoad() instead of OnServerInitialized for this?
     
  14. For instance, because i don't want to start the timer before the server loads.

    Of course there are ways to do it in another way and avoid this. But the only reason why i posting this is because i think it could be a bug.

    Once plugin is unloaded it is also being removed from plugin manager and hook subscriptions, and therefore Oxide shouldn't call OnServerInitialized on it after that.
    But somehow it happens.
    [DOUBLEPOST=1438360293][/DOUBLEPOST]Alright, got it! It is not a bug.
    Loaded and Init hooks is called from HandleAddedToManager methods.
    Forcing unload of plugin at this moment causes it to unload in the middle of loading process and the shit will happen.

    Simply said, there is no way to unload plugin earlier than OnServerInitialized / OnPluginLoaded is called.

    Remember this and do not repeat my mistake :)