Connecting Tech Pros Worldwide Forums | Help | Site Map

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

groleo@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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();
};


Niels Dybdahl
Guest
 
Posts: n/a
#2: Jul 23 '05

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



<groleo@gmail.com> wrote in message
news:1110786496.073305.327340@g14g2000cwa.googlegr oups.com...[color=blue]
> 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;[/color]

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


groleo@gmail.com
Guest
 
Posts: n/a
#3: Jul 23 '05

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


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

Karl Heinz Buchegger
Guest
 
Posts: n/a
#4: Jul 23 '05

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


"groleo@gmail.com" wrote:[color=blue]
>
> 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;[/color]

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


--
Karl Heinz Buchegger
kbuchegg@gascad.at
groleo@gmail.com
Guest
 
Posts: n/a
#5: Jul 23 '05

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


The whole source code is at
http://groleo.go.ro/ttl/1.htm.
The Sock* sock, variable does go out of scope.
I tried to declare it as global, but then I would
get into multiple declarations.

Karl Heinz Buchegger
Guest
 
Posts: n/a
#6: Jul 23 '05

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


"groleo@gmail.com" wrote:[color=blue]
>
> The whole source code is at
> http://groleo.go.ro/ttl/1.htm.[/color]

Well.
I can't access that server, it doesn't seem
to exist.

What's so hard about creating a small test program
which shows your problem? In the process of creating
it, you might even discover the root of your problem
(you wouldn't be the first one).

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Bogdan Sintoma
Guest
 
Posts: n/a
#7: Jul 23 '05

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



groleo@gmail.com wrote:[color=blue]
> Nop, i've commented out , the Sock* from class Handler.[/color]

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

groleo@gmail.com
Guest
 
Posts: n/a
#8: Jul 23 '05

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


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.

Closed Thread