I am developing a position based multcast protocol.I am using getLoc(double x......) method in the MobileNode class of mobilenode.cc file to find the position of the node.In the MobileNode constructor the variables X_,Y_,and Z_ are initialized to 0.0 and i found that this part of the code is working fien by using fprintf().But iside the getLoc() method or update_position() method i am getting segmentation fault if I trying to access variables X_,Y_Z_,position_update_time_ etc.
I think my problem is, I have not created an object of MobileNode class before calling getLoc method.In my agent class i declared a variable MobileNode mn_ and claaed the getLoc method using mn_->getLoc(&x,&y,&z).I am working on this problem from the last week and not getting the correct result.If my guess is correct please tell me where to create the MobileNode object.Otherwise tell me the reason of this sementation fault.
More details of error...
in my agent class
In my agent implemntation class
fprintf(stderr, "Listen to me HELLO.....\n");
Packet *p = Packet::alloc();
position *ps = getMyPosition();
myx = myy = myz = 0.0;
//fprintf(stderr, "Declaration is fine....\n");
mn_->getLoc(&myx, &myy, &myz); //========>>>>Here is the segmentation fault
fprintf(stderr, "getMyPosition() is called....\n");
pos->x = myx;
pos->y = myy;
pos->z = myz;
The getLoc method in mobilenode.cc is below.
void MobileNode::getLoc(double *x, double *y, double *z)
fprintf(stderr, "getLoc() is called....%f\n");//,X_);
//fprintf(stderr,"positin update time fro getLoc()%f \n",position_update_time_);
fprintf(stderr, "After update_position() ....\n");
*x = X_;
*y = Y_;
*z = Z_;
If i am trying to print X_,position_update_time_ etc the result is segmentation fault...
Please help me.
Re: segmentation fault in getLoc method in MobileNode class in NS2
I corrected the mistake.My guess was correct.I included following line in the getMyPosition() method.
Node* nd = Node::get_node_by_address(mr_addr());
mn_ = (MobileNode*)nd;
Here "mr_addr()" is an inline function in my agent class that returns the address of agent.
Now the segmentation fault is over and its creating a trace file with details of Hello packet.