views:

208

answers:

1

I'm working on a plugin for a smaller application using gtkmm. The plugin I'm working on checks certain conditions (the date had changed and a new day started) after every minute and starts some actions if the conditions are true. In the initialization part of the plugin I have the following piece of code that uses Glib::SignalTimeout and sigc++:

testCounter = 0;
sigc::slot<bool> tslot = sigc::mem_fun(*this, 
               &NoteOfDayFactory::checkNewDay);
timeoutObj = Glib::signal_timeout()
              .connect(tslot,CHECK_INTERVAL);

where testCounter is an attribute defined in the class that contains the initialization method and CHECK_INTERVAL is a constant equal to 1 minute. All the other variables present are defined in the class that contains the initialization code and the callback method. The checkNewDay method is where the condition is tested and action taken if the day had changed:

bool NoteOfDayFactory::checkNewDay() {
    std::cout << "Checking for new day every minute or so" << std::endl;
    std::cout << "Before incrementing" << std::endl;
    for(int i = 0; i < 100000; i++);
    counter++;
    std::cout << counter << " minutes elapsed" << std::endl;
    return true; }

I put the small test code, presented above, before I used the real action, to test if everything goes well and the checkNewDay isn't called more than once every minute. What I found puzzle me. After every minute elapses I get a number of let say 10 messages (at least) printed on the stdout but the variable increases only once every minute.

** snip ****

Checking for new day every minute or so

Before incrementing

1 minutes elapsed

Checking for new day every minute or so

Before incrementing

1minutes elapsed

** snip **

Checking for new day every minute or so

Before incrementing

2 minutes elapsed

Checking for new day every minute or so

Before incrementing

2 minutes elapsed

** snip ****

It behaves like the text was sent to 10 (or so) different buffers and printed out at once after every minute. Could somebody enlighten me and help me understand why is this happening, because I'm pretty sure that the callback is called only once every minute. Thank you!

+1  A: 

Hi,

I've tried to reproduce with the following code :

#include <iostream>
#include <glibmm.h>

unsigned counter = 0;

bool checkNewDay()
{
    std::cout << "Checking for new day ..." << std::endl;
    counter++;
    std::cout << "counter = " << counter << std::endl;

    return true;
}


int main()
{
    static const unsigned delayInMillis = 1000;

    sigc::slot<bool> tslot = sigc::ptr_fun(&checkNewDay);
    Glib::signal_timeout().connect(tslot, delayInMillis);

    std::cout << "Starting Glib::MainLoop" << std::endl;
    Glib::MainLoop::create(false)->run();
}

It outputs (as expected) :

Starting Glib::MainLoop
Checking for new day ...
counter = 1
Checking for new day ...
counter = 2
Checking for new day ...
counter = 3
Checking for new day ...
counter = 4

I suggest you try to reproduce with a simple example like mine , by eventually adding what is specific to your code.

I can not see why it should not work, unless perhaps if you call some Glib / Gtkmm methods that process events many times !? How is your main loop ?

I know this is not much help, but it works for me ...

neuro
I reproduced something similar to your code and it works as expected. I do not have access to the main loop because I'm writing a plugin to a application created by others.In the plugin it looks like the callback is called more than once on timeout. What puzzle me is that the variable is not incremented every time only when it suppose to (see in the print out listing). This is why I suppose that the callback is called only once but the output is somehow duplicated.This is why I asked the question because I don't quite understand what is causing this.I will try see the apps source code.
crazybyte
@crazy: maybe a log problem. I worked with libs like log4cplus. Based on configuration, those log systesm might duplicate logs. There is perhaps something like that as the output is, I agree, really weird. Check the log system ...
neuro