Help with cancelAndDelete()

48 views
Skip to first unread message

wli

unread,
Aug 18, 2014, 2:54:08 AM8/18/14
to omn...@googlegroups.com
Hi, I was having problems with cancelAndDelete(msg), I found out it was because the message had expired before the point of cancelAndDelete() so it crashes the simulation. Now I am doing it in this way first check if the message is scheduled then cancel and delete.
if(msg->isScheduled()) {cancelAndDelete(msg);}
But my simulation still crashes and in the debug mode the crash terminates at the isScheduled() function in cMessage.h. So I don't understand what is reason. Any help is appreciated. Thanks.

Michael Kirsche

unread,
Aug 18, 2014, 3:21:18 AM8/18/14
to omn...@googlegroups.com
"The message had expired before ..."
Did you actually delete the message before calling isScheduled?

wli

unread,
Aug 18, 2014, 3:29:54 AM8/18/14
to omn...@googlegroups.com
I can't be sure if the message got deleted before calling isScheduled because I have this if block at different places so I am thinking it could be that the message is deleted at one place and then I call isScheduled at another place. I am confused. What is the safe way to use cancelAndDelete()?

wli

unread,
Aug 18, 2014, 3:32:29 AM8/18/14
to omn...@googlegroups.com
But when I only have this if block at one place it still caused trouble. So if I need to use cancelAndDelete() safely what should I do? thanks

Michael Kirsche

unread,
Aug 18, 2014, 3:44:25 AM8/18/14
to omn...@googlegroups.com
In general, I am speaking about timer messages here, because those are scheduled for transmission and checked if they are SelfMessages.
Transmission of packets and deleting packets happens in different cases, although both are represented by messages.

cancelAndDelete(msg) should be used for timers in the destructor of a module.
Normal packets are set to NULL and then deleted.
Timers are not actually deleted, just canceled.
So what are you talking about, timers or data packets/messages??

wli

unread,
Aug 18, 2014, 4:55:39 AM8/18/14
to omn...@googlegroups.com
timers. It's a self message that I scheduled.  I tried this (timerStart is the self message I scheduled.)
 if (timerStart != NULL) {
                    if (timerStart->isScheduled()) {
                        cancelAndDelete(timerStart);

                    }

                }
but it still fails at isScheduled call.

Michael Kirsche

unread,
Aug 18, 2014, 5:02:45 AM8/18/14
to omn...@googlegroups.com
Can you post/send the file?
Hard to say without looking at the complete code why your call to isScheduled is breaking...

wli

unread,
Aug 18, 2014, 6:37:23 AM8/18/14
to omn...@googlegroups.com
Hi, to make it short here is how I scheduled the message:
void HeteroRoute::initialize(int stage)
{
/** @brief schedule timer start: time for starting as CH.   * */
        /** @brief schedule timer based on pwrSrc value. **/
        double copy_PwrSrc = myCorrTbl.pwrSrc;
        double schedule_Delay = exp2(1/copy_PwrSrc);
        cMessage *timerStart = new cMessage("timerStart", TIMERSTART);
        //assert(timerStart);
        scheduleAt(simTime() + schedule_Delay + uniform(0.1415926, 0.618), timerStart );
        debugEV<<"Scheduling a self message "<<timerStart->getName()<<" \n"<<endl;
}
And I am using cancelAndDelete inside handleLowerMsg() like:
void HeteroRoute::handleLowerMsg(cMessage* msg) {
/****************************************/

if (timerStart != NULL) { if (timerStart->isScheduled()) { cancelAndDelete(timerStart); } }
/*************************************/
}
Thanks for your help.
Reply all
Reply to author
Forward
0 new messages