Signal handler function - pointer to this gets lost

This is a discussion on Signal handler function - pointer to this gets lost within the C++ Programming forums, part of the General Programming Boards category; The Class I'm currently writing should do the following: Setting up a POSX timer (OK), setting up a signal-handler function ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Signal handler function - pointer to this gets lost

    The Class I'm currently writing should do the following:

    Setting up a POSX timer (OK), setting up a signal-handler function (OK) which calls a member function and therefore uses the this-pointer (not OK).
    Code:
    void Scheduler::setUp() {
    	// interval occurrence
    	this->timerSpecs.it_interval.tv_sec = 0;
    	this->timerSpecs.it_interval.tv_nsec = HARDCODEDSCHINTERVALL;
    	// First occurrence
    	this->timerSpecs.it_value.tv_sec = 0;
    	this->timerSpecs.it_value.tv_nsec = HARDCODEDSCHINTERVALL;
    
    	// Define sigaction method
    	this->SignalAction.sa_sigaction = Scheduler::tic;  // the static signal handler function 
    	// set mask empty -> react on all signals
    	sigemptyset(&this->SignalAction.sa_mask);
    	this->SignalAction.sa_flags = SA_SIGINFO;
    
    	// Define sigEvent
    	this->signalEvent.sigev_value.sival_ptr = this;
    
    	if (timer_create(CLOCK_REALTIME, NULL /*&this->signalEvent*/,
    			&this->timerID) != 0) { // timer id koennte mit private probleme geben
    		// TODO: throw Exception
    		exit(1);
    	}
    
    	// install tic as signal handler
    	if (sigaction(SIGALRM, &this->SignalAction, NULL)) {
    		perror("Could not install new signal handler");
    	}
    }
    // Class layout
    class Scheduler {
    public:
    	Scheduler();
    	virtual ~Scheduler();
    
    	timer_t timerID;
    	
    	sigset_t SigBlockSet;
    
    	struct sigevent signalEvent;
    	
    	struct sigaction SignalAction;
    
    	struct itimerspec timerSpecs;
    
    	typedef pair<int, InfClockListener&> wakeUpCandidate;
    
    	/**
    	 * Vector containing the Input Devices which want to be waken up
    	 */
    	vector<wakeUpCandidate *> wakeUpCandidates;
    
    	int debug;
    
             /**
    	 * Add an InputDevice to the scheduler
    	 * @param interval Read-Out Intervall in Hz (n/seconde)
    	 * @param listener The reference to the input object
    	 */
    	void wakeMeUp(int interval, InfClockListener& listener);
    
    private:
    	/**
    	 * Smallest possible time event RT.
    	 * Fired by the SIGALRM due timer stop
    	 * Checks the wakeUpCadidates and fires them up.
    	 */
    	static void tic(int sigNumb, siginfo_t *si, void *uc);
    
    	/**
    	 * Prepares all necessary stuff (timer, blocking set usw.)
    	 */
    	void setUp();
    
    };
    As you can see in the function setUp(), I store the this-pointer into the sigev_value to pass it to the signal-handler function.

    This function looks like this:
    Code:
    void Scheduler::tic(int sigNumb, siginfo_t *si, void *uc) {
    	core::Scheduler * ptrScheduler =
    			reinterpret_cast<Scheduler *> (si->si_value.sival_ptr); // points to something other than I stored!
            int debugInt;
    	debugInt = ptrScheduler->debug; // contains the wrong value stored in a public member variable
    	debugInt= ptrScheduler->wakeUpCandidates.size();  // returns the incorrect size 3931200
    
    }
    Now the problem starts: During the signal-handler function I can us the passed pointer to access member function in the class Scheduler. That confuses me, because the pointer to the Scheduler Object is not the same at is was during the signal setupUp() function. Although I can access member of the class Scheduler through this new pointer but all I get are wrong random values.

    Where gets the si->si_value.sival_ptr changed? What I'm doing wrong?

    thanks for your help.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by maus View Post
    Where gets the si->si_value.sival_ptr changed? What I'm doing wrong?
    What you're doing wrong is trying to store something in the si_value field.

    You need to stash the pointer in a volatile global variable instead.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21