How can i find when a variable is out of scope?

G

groleo

Hi.
I have some problems with variables :D

I have the classes given below:

The problem is when I try to acces Commands().sock->m_sock, from
inside Message();
When I'm using m_sock inside Commands(), it has one value.
When I'm using m_sock inside Message() it has another value.
The thing is that i'm not modifying the m_sock value;


class Socket {
public:
Socket(){};
virtual ~Socket(){};

bool create();
bool bind ( const int port );
bool listen() const;
bool accept ( Socket& ) const;
bool connect ( const std::string host, const int port
);

// Data Transimission
bool send ( const std::string ) const;
int recv ( std::string& ) const;
void set_non_blocking ( const bool );
bool is_valid() const { return m_sock != -1; }

private:
int m_sock;
sockaddr_in m_addr;


};




class Handler {
public:
Handler() {} ;
virtual ~Handler(){};
virtual void OnStartElement(){};
virtual void OnElementEnd(){};
virtual void OnCharacter(){};
Socket* sock;
};

class Commands : public Handler {
public:
Commands();
virtual ~Commands() {};
void OnStartElement();
void OnElementEnd();
void OnCharacter();
Socket* sock;
};

class Message : public Handler {
public:
Message();
virtual ~Message() {};
void OnStartElement();
void OnElementEnd();
void OnCharacter();
};
 
N

Niels Dybdahl

Hi.
I have some problems with variables :D

I have the classes given below:

The problem is when I try to acces Commands().sock->m_sock, from
inside Message();
When I'm using m_sock inside Commands(), it has one value.
When I'm using m_sock inside Message() it has another value.
The thing is that i'm not modifying the m_sock value;

You have two Socket* sock; one in Handler and one in Commands. Is this on
purpose or is that the error ?
The cause might be that not both are initialized.

Niels Dybdahl
 
K

Karl Heinz Buchegger

Hi.
I have some problems with variables :D

I have the classes given below:

The problem is when I try to acces Commands().sock->m_sock, from
inside Message();
When I'm using m_sock inside Commands(), it has one value.
When I'm using m_sock inside Message() it has another value.
The thing is that i'm not modifying the m_sock value;

Please show the exact code that implements this.
If possible, create a short, complete test program which shows just
that error in action.
 
B

Bogdan Sintoma

Nop, i've commented out , the Sock* from class Handler.

Are you sure? ;)
In that case you don't have any sock member in Message class. So, haw
could it have any value?
And by the way there are a lot of wired stuff in
Message::OnStartElement(). You create and use temporary un-initialized
Commands objects.

As Karl already told you, please make a short test program that has the
same problem.

Bogdan Sintoma
 
G

groleo

Well, done the test:)
The problem was , as Karl said, in
Message::OnStartElement().
I was initializing a new object, and the
socket file descriptor was re-newed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top