Re: Problem when calling commands from within a TaskContext

Peter Soetens <[email protected]> Thu, 24 May 2007 13:07:03 +0200
Newsgroups gmane.science.robotics.orocos.user
Organization FMTC
Message-ID <[email protected]>
On Thursday 24 May 2007 11:33:18 Lars-Peter Ellekilde wrote:

>
>     void update() {
>         RTT::TaskContext* task = getPeer("Task1");
>         RTT::Command<bool(void)> cmd =
> task->commands()->getCommand<bool(void)>("mycmd");
>         bool b = cmd();

Commands are asynchronous. This means that after cmd() has been called,
a thread is scheduled to execute the command function. If you immediately
destroy the cmd object before the function could be executed, the thread will
execute a no longer existing object, and you get your segfault. If this were a 
method, this error would not have occured, as it is executed directly. 

See the comment of Ruben for the correct solution.

Peter

>     }
>
>     void shutdown() {}
>
> };
> **
>  * main() function
>  */
> int ORO_main(int arc, char* argv[])
> {
>
>     Task1 task1("Task1");
>     Task2 task2("Task2");
>
>     connectPeers(&task1, &task2);
>
>     PeriodicActivity activity1(OS::HighestPriority, 0.01, task1.engine() );
>     activity1.start();
>
>     PeriodicActivity activity2(OS::LowestPriority, 1, task2.engine() );
>     activity2.start();
>
>     while (true) sleep(10);
>
>     return 0;
> }



-- 
Peter Soetens -- FMTC -- <http://www.fmtc.be>
-- 
Orocos mailing list
[email protected]
http://lists.mech.kuleuven.be/mailman/listinfo/orocos