Global variable seg fault
I am getting a segmentation fault and I 开发者_运维问答cannot figure out why. I have a global TcpClient object that has a pointer to an Agent object. I am trying to access the agent pointer in a function when the segmentation fault occurs. In main, I have the cout statements before and after I set the agent member in TcpClient and both statements give me the same address.
TcpClient client((char*)PORT);
Agent* agent = new Agent;
int main(int argc, char* args[]) {
//initialization code for agent's members
cout<<"\nagent: "<<agent;
client.setAgent(agent); //set it here
cout<<"\nclient agent: "<<client.getAgent()<<"\n";
}
Then I have this function in TcpClient that gets called during the run (after setting the agent of course). I get the segmentation fault when I try to access agent. I have a cout statement in the beginning that tells me agent is 0x0.
void TcpClient::getCommand(char* command) {
std::cout<<"\nagent: "<<agent;
}
The setAgent is a typical setter -
void TcpClient::setAgent(Agent*& a) {agent = a;}
class Agent;
class TcpClient {
//functions and stuff
private:
Agent* agent;
};
The agent member isn't accessed anywhere else in the code. The only thing I can come up with is that there is something about the TcpClient object being global that could make this happen, but I do not know what. Am I right about this? Any help is appreciated.
When I run the code, the debugger says -
Program received signal SIGSEGV, Segmentation fault.
__memcpy_ssse3_rep () at ../sysdeps/i386/i686/multiarch/memcpy-ssse3-rep.S:1454
1454 ../sysdeps/i386/i686/multiarch/memcpy-ssse3-rep.S: No such file or directory.
in ../sysdeps/i386/i686/multiarch/memcpy-ssse3-rep.S
(gdb) back
#0 __memcpy_ssse3_rep ()
at ../sysdeps/i386/i686/multiarch/memcpy-ssse3-rep.S:1454
#1 0x08049c52 in Agent::setGoal (this=0x0, g=...) at agent.cpp:33
#2 0x0805075e in TcpClient::getCommand (this=0x805aac8,
command=0xbffff18d "1 3 1\n") at tcpclient.cpp:80
#3 0x08050b8d in TcpClient::communicate (this=0x805aac8) at tcpclient.cpp:153
#4 0x0804e0f8 in main (argc=1, args=0xbffff3f4) at mainclient.cpp:119
You should read the compiler/linker warnings, I'm fairly sure it'll be throwing some at you about ambiguous names.
Do not use "agent" as a variable name for class members when you've already defined it earlier as a global; use something like private: Agent* myAgent;
(and of course change setAgent to use it) and you should hopefully find the issue goes away.
Additionally, *& basically cancels itself out; the private member is a pointer so you should be using a pointer in the set method - I suspect your segfault is because of a bad operation on either what it thinks is a pointer and actually isn't or vice versa.
I have a function called communicate (which just lets server and client talk to each other) that calls the getCommand function I posted above. Right before I call getCommand, there was a memset call for a local char*. All I did was remove that line and the segmentation fault went away.
Thanks for help everyone.
Is void TcpClient::setAgent(Agent*& a) {agent = a;}
written as intended, or did you mean Agent *a
?
If you pass an Agent
object, the syntax is Agent &a
, which means "a
is the address of the passed Agent
object."
If you pass a pointer (which you're doing here), the syntax is Agent *a
, which means "a
is passed as a (possibly null) pointer to an Agent
object."
精彩评论