Thanks All for support,

There were no more errors in the code after your help.
The error was now in the CAN interface of the machine that was having trouble clearing the message queue.