Thread: [Dev-C++] segmentation fault at runtime
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Black E. <bla...@ea...> - 2004-12-15 20:30:05
|
Hi guys!
I got a problem at run time, when using a pointer in a function.
The error is in the function KviIrcServer* server() at line : return m_pSrv;
In all other functions I got no problem with this variable. It is another
class that tries to call this function, it's the friendclass beChannel
Here is the beConsole.h
#ifndef BE_CONSOLE_H
#define BE_CONSOLE_H
#include "beMDIWindow.h"
class KviIrcServer;
class beConsole : beMDIWindow
{
friend class beIRC;
friend class beChannel;
Q_OBJECT
public :
beConsole(QWidget * parent = 0, const char * name = 0, WFlags f = 0);
~beConsole();
KviIrcServer* server();
protected :
bool connectToServer();
private slots :
virtual void hostFound();
virtual void connected();
virtual void socketReadyRead();
virtual void disconnected();
void send(const QString & command);
private :
KviIrcServer * m_pSrv;
// KviProxy * prx;
QSocket * m_pQSocket;
};
#endif
And here is the beConsole.cpp
// beIRC stuff
#include "beConsole.h"
// kvirc stuff
#define HAVE_CONFIG_H
#include "config.h"
#include <kvirc/3.0.1/kvi_app.h>
#include <kvirc/3.0.1/kvi_ircserverdb.h>
#include <kvirc/3.0.1/kvi_confignames.h>
// QT Stuff
#include <qregexp.h>
#include <qsocket.h>
extern KVIRC_API KviIrcServerDataBase * g_pIrcServerDataBase;
beConsole::beConsole(QWidget * parent,const char * name, WFlags f) :
beMDIWindow(parent,name,f)
{
m_windowType = beMDIWindow::Console;
KviIrcNetwork * net = g_pIrcServerDataBase->currentNetwork();
if(net)m_pSrv = net->currentServer();
QString srvname = m_pSrv->hostname();
setCaption(srvname);
connect ( this, SIGNAL( userInput(const QString &) ), this, SLOT ( send(const
QString &) ) );
}
beConsole::~beConsole()
{
delete m_pSrv;
}
bool beConsole::connectToServer()
{
if(!m_pSrv) { return false; }
// QString text = "Connect to server";
// m_pLineView->addLine(text);
// \n for new liens
// \t for tabs
append("<mytag>Connecting to server...</mytag>");
m_pQSocket = new QSocket(this);
connect( m_pQSocket, SIGNAL( hostFound() ), this, SLOT( hostFound() ) );
connect( m_pQSocket, SIGNAL( connected() ), this, SLOT( connected() ) );
connect( m_pQSocket, SIGNAL( readyRead() ), this, SLOT(socketReadyRead()) );
connect( m_pQSocket, SIGNAL( connectionClosed() ), this,
SLOT( disconnected() ));
m_pQSocket->connectToHost( m_pSrv->hostname(), m_pSrv->port() );
return true;
}
void beConsole::hostFound()
{
append("<mytag>Host found ...</mytag>");
}
void beConsole::connected()
{
append("<mytag>Connected to "+m_pSrv->hostname()+"</mytag>");
QString cmd;
cmd = "USER alex 0 "+m_pSrv->hostname()+" :Using beIRC 0.0.1\n";
m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
cmd = "NICK blackeagle654\n";
m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
}
void beConsole::socketReadyRead()
{
// append("<mytag>Socket is ready to read</mytag>");
while ( m_pQSocket->canReadLine() ) {
QString msg = m_pQSocket->readLine();
QRegExp rxA("(.*)\n$");
rxA.search( msg );
append( rxA.cap(1) );
/* BEGIN ping pong fast reply */
QRegExp rx("^PING :([0-9]+)");
int pos;
if( (pos = rx.search(msg)) != -1)
{
QString cmd = "PONG :"+rx.cap(1)+"\n";
m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
}
/* END ping pong fast reply */
QRegExp rx2("376.*End of /MOTD command");
if( (pos = rx2.search(msg)) != -1)
{
QString cmd = "JOIN #gentoo\n";
m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
}
}
}
void beConsole::disconnected()
{
append("<mytag>Disconnected from server</mytag>");
}
void beConsole::send(const QString & command)
{
append("<command>"+command+"</command>");
// command.append("\n");
QString tmp = command+"\n";
m_pQSocket->writeBlock( tmp.latin1(), tmp.length() );
}
KviIrcServer* beConsole::server()
{
qDebug("returning server");
return m_pSrv;
}
Thanks for any help !
|
|
From: Alfred P. R. <al...@ya...> - 2004-12-15 21:06:28
|
Hi Black Eagle,
It could be that you are returning the address of m_pSrv rather than it's contents. You've declared m_pSrv as a pointer to a KviIrcServer class. However, in server(), you return m_pSrv, rather than it's contents *m_pSrv. server() is declared as returning a pointer to the class, which is the value of m_pSrv, not it's address.
Hope that makes sense!
Black Eagle <bla...@ea...> wrote:
Hi guys!
I got a problem at run time, when using a pointer in a function.
The error is in the function KviIrcServer* server() at line : return m_pSrv;
In all other functions I got no problem with this variable. It is another
class that tries to call this function, it's the friendclass beChannel
<snip>
class KviIrcServer;
<snip>
KviIrcServer* server();
<snip>
private :
KviIrcServer * m_pSrv;
<snip>
KviIrcServer* beConsole::server()
{
qDebug("returning server");
return m_pSrv;
}
<snip>
Alfred P. Reaud
|
|
From: Black E. <bla...@ea...> - 2004-12-15 21:42:49
|
It's tricky but I think I got it
In fact I am returning a pointer to m_pSrv which is a pointer itself, rathe=
r=20
than the adress of the class, right ?
What could have worked is if m_pSrv was the class and that I returned &m_pS=
rv,=20
the adress of the class.
But I've done like this :
KviIrcServer beConsole::server()
{
qDebug("returning server");
return *m_pSrv;
}
Thanks for help !
Le mercredi 15 D=E9cembre 2004 22:06, Alfred P. Reaud a =E9crit :
> Hi Black Eagle,
>
> It could be that you are returning the address of m_pSrv rather than it's
> contents. You've declared m_pSrv as a pointer to a KviIrcServer class.=20
> However, in server(), you return m_pSrv, rather than it's contents *m_pSr=
v.
> server() is declared as returning a pointer to the class, which is the
> value of m_pSrv, not it's address.
>
> Hope that makes sense!
>
> Black Eagle <bla...@ea...> wrote:
>
> Hi guys!
>
> I got a problem at run time, when using a pointer in a function.
>
> The error is in the function KviIrcServer* server() at line : return
> m_pSrv;
>
> In all other functions I got no problem with this variable. It is another
> class that tries to call this function, it's the friendclass beChannel
>
>
> <snip>
> class KviIrcServer;
> <snip>
> KviIrcServer* server();
> <snip>
> private :
> KviIrcServer * m_pSrv;
> <snip>
> KviIrcServer* beConsole::server()
> {
> qDebug("returning server");
> return m_pSrv;
> }
> <snip>
>
>
>
>
> Alfred P. Reaud
|
|
From: Per W. <pw...@ia...> - 2004-12-15 21:55:34
|
Wait a minute.
m_pSrv is a pointer pointing to an object.
KviIrcServer* beConsole::server()
should return a pointer, i.e. the contents of m_pSrv, i.e.
return m_pSrv;
and not a copy of an object as is done by
return *m_pSrv;
When a function returns a pointer, this of course doesn't mean that it
returns the address of the pointer, but the value stored in the pointer,
i.e. the address of an object.
/Per W
On Wed, 15 Dec 2004, Black Eagle wrote:
> It's tricky but I think I got it
>
> In fact I am returning a pointer to m_pSrv which is a pointer itself, rat=
her
> than the adress of the class, right ?
>
> What could have worked is if m_pSrv was the class and that I returned &m_=
pSrv,
> the adress of the class.
>
> But I've done like this :
> KviIrcServer beConsole::server()
> {
> =09qDebug("returning server");
> =09return *m_pSrv;
> }
>
> Thanks for help !
>
> Le mercredi 15 D=E9cembre 2004 22:06, Alfred P. Reaud a =E9crit :
> > Hi Black Eagle,
> >
> > It could be that you are returning the address of m_pSrv rather than it=
's
> > contents. You've declared m_pSrv as a pointer to a KviIrcServer class.
> > However, in server(), you return m_pSrv, rather than it's contents *m_p=
Srv.
> > server() is declared as returning a pointer to the class, which is the
> > value of m_pSrv, not it's address.
> >
> > Hope that makes sense!
> >
> > Black Eagle <bla...@ea...> wrote:
> >
> > Hi guys!
> >
> > I got a problem at run time, when using a pointer in a function.
> >
> > The error is in the function KviIrcServer* server() at line : return
> > m_pSrv;
> >
> > In all other functions I got no problem with this variable. It is anoth=
er
> > class that tries to call this function, it's the friendclass beChannel
> >
> >
> > <snip>
> > class KviIrcServer;
> > <snip>
> > KviIrcServer* server();
> > <snip>
> > private :
> > KviIrcServer * m_pSrv;
> > <snip>
> > KviIrcServer* beConsole::server()
> > {
> > qDebug("returning server");
> > return m_pSrv;
> > }
> > <snip>
> >
> >
> >
> >
> > Alfred P. Reaud
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://productguide.itmanagersjournal.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Per W. <pw...@ia...> - 2004-12-15 21:51:14
|
Are you sure that the beChannel object really have a valid pointer to your
beMDIWindow object.
Also, note that qDebug() which I don't know what it does might be
dangerous in case you have a multi-threaded application. Only the thread
that owns the MDI window should try and used any GDI objects.
/Per W
On Wed, 15 Dec 2004, Black Eagle wrote:
> Hi guys!
>
> I got a problem at run time, when using a pointer in a function.
>
> The error is in the function KviIrcServer* server() at line : return m_pSrv;
>
> In all other functions I got no problem with this variable. It is another
> class that tries to call this function, it's the friendclass beChannel
>
>
> Here is the beConsole.h
>
> #ifndef BE_CONSOLE_H
> #define BE_CONSOLE_H
>
> #include "beMDIWindow.h"
>
> class KviIrcServer;
>
> class beConsole : beMDIWindow
> {
> friend class beIRC;
> friend class beChannel;
> Q_OBJECT
> public :
> beConsole(QWidget * parent = 0, const char * name = 0, WFlags f = 0);
> ~beConsole();
>
> KviIrcServer* server();
> protected :
> bool connectToServer();
> private slots :
>
> virtual void hostFound();
> virtual void connected();
> virtual void socketReadyRead();
> virtual void disconnected();
> void send(const QString & command);
> private :
> KviIrcServer * m_pSrv;
> // KviProxy * prx;
>
> QSocket * m_pQSocket;
> };
>
> #endif
>
> And here is the beConsole.cpp
> // beIRC stuff
> #include "beConsole.h"
> // kvirc stuff
> #define HAVE_CONFIG_H
> #include "config.h"
> #include <kvirc/3.0.1/kvi_app.h>
> #include <kvirc/3.0.1/kvi_ircserverdb.h>
> #include <kvirc/3.0.1/kvi_confignames.h>
> // QT Stuff
> #include <qregexp.h>
> #include <qsocket.h>
>
> extern KVIRC_API KviIrcServerDataBase * g_pIrcServerDataBase;
>
> beConsole::beConsole(QWidget * parent,const char * name, WFlags f) :
> beMDIWindow(parent,name,f)
> {
> m_windowType = beMDIWindow::Console;
>
> KviIrcNetwork * net = g_pIrcServerDataBase->currentNetwork();
>
> if(net)m_pSrv = net->currentServer();
> QString srvname = m_pSrv->hostname();
> setCaption(srvname);
> connect ( this, SIGNAL( userInput(const QString &) ), this, SLOT ( send(const
> QString &) ) );
> }
>
> beConsole::~beConsole()
> {
> delete m_pSrv;
> }
>
> bool beConsole::connectToServer()
> {
> if(!m_pSrv) { return false; }
> // QString text = "Connect to server";
> // m_pLineView->addLine(text);
> // \n for new liens
> // \t for tabs
> append("<mytag>Connecting to server...</mytag>");
>
> m_pQSocket = new QSocket(this);
> connect( m_pQSocket, SIGNAL( hostFound() ), this, SLOT( hostFound() ) );
> connect( m_pQSocket, SIGNAL( connected() ), this, SLOT( connected() ) );
> connect( m_pQSocket, SIGNAL( readyRead() ), this, SLOT(socketReadyRead()) );
> connect( m_pQSocket, SIGNAL( connectionClosed() ), this,
> SLOT( disconnected() ));
> m_pQSocket->connectToHost( m_pSrv->hostname(), m_pSrv->port() );
> return true;
> }
>
>
> void beConsole::hostFound()
> {
> append("<mytag>Host found ...</mytag>");
> }
>
> void beConsole::connected()
> {
> append("<mytag>Connected to "+m_pSrv->hostname()+"</mytag>");
> QString cmd;
> cmd = "USER alex 0 "+m_pSrv->hostname()+" :Using beIRC 0.0.1\n";
> m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
> cmd = "NICK blackeagle654\n";
> m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
> }
>
> void beConsole::socketReadyRead()
> {
> // append("<mytag>Socket is ready to read</mytag>");
> while ( m_pQSocket->canReadLine() ) {
>
> QString msg = m_pQSocket->readLine();
> QRegExp rxA("(.*)\n$");
> rxA.search( msg );
> append( rxA.cap(1) );
> /* BEGIN ping pong fast reply */
> QRegExp rx("^PING :([0-9]+)");
> int pos;
> if( (pos = rx.search(msg)) != -1)
> {
> QString cmd = "PONG :"+rx.cap(1)+"\n";
> m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
> }
> /* END ping pong fast reply */
> QRegExp rx2("376.*End of /MOTD command");
> if( (pos = rx2.search(msg)) != -1)
> {
> QString cmd = "JOIN #gentoo\n";
> m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
> }
> }
> }
>
> void beConsole::disconnected()
> {
> append("<mytag>Disconnected from server</mytag>");
> }
>
> void beConsole::send(const QString & command)
> {
> append("<command>"+command+"</command>");
> // command.append("\n");
> QString tmp = command+"\n";
> m_pQSocket->writeBlock( tmp.latin1(), tmp.length() );
> }
>
> KviIrcServer* beConsole::server()
> {
> qDebug("returning server");
> return m_pSrv;
> }
>
>
> Thanks for any help !
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://productguide.itmanagersjournal.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Per W. <pw...@ia...> - 2004-12-15 21:57:08
|
One more thing. If g_pIrcServerDataBase->currentNetwork() returns NULL,
you will never assign any value to your m_pSrv member within the
beConsole() constructor.
/Per W
On Wed, 15 Dec 2004, Black Eagle wrote:
> Hi guys!
>
> I got a problem at run time, when using a pointer in a function.
>
> The error is in the function KviIrcServer* server() at line : return m_pSrv;
>
> In all other functions I got no problem with this variable. It is another
> class that tries to call this function, it's the friendclass beChannel
>
>
> Here is the beConsole.h
>
> #ifndef BE_CONSOLE_H
> #define BE_CONSOLE_H
>
> #include "beMDIWindow.h"
>
> class KviIrcServer;
>
> class beConsole : beMDIWindow
> {
> friend class beIRC;
> friend class beChannel;
> Q_OBJECT
> public :
> beConsole(QWidget * parent = 0, const char * name = 0, WFlags f = 0);
> ~beConsole();
>
> KviIrcServer* server();
> protected :
> bool connectToServer();
> private slots :
>
> virtual void hostFound();
> virtual void connected();
> virtual void socketReadyRead();
> virtual void disconnected();
> void send(const QString & command);
> private :
> KviIrcServer * m_pSrv;
> // KviProxy * prx;
>
> QSocket * m_pQSocket;
> };
>
> #endif
>
> And here is the beConsole.cpp
> // beIRC stuff
> #include "beConsole.h"
> // kvirc stuff
> #define HAVE_CONFIG_H
> #include "config.h"
> #include <kvirc/3.0.1/kvi_app.h>
> #include <kvirc/3.0.1/kvi_ircserverdb.h>
> #include <kvirc/3.0.1/kvi_confignames.h>
> // QT Stuff
> #include <qregexp.h>
> #include <qsocket.h>
>
> extern KVIRC_API KviIrcServerDataBase * g_pIrcServerDataBase;
>
> beConsole::beConsole(QWidget * parent,const char * name, WFlags f) :
> beMDIWindow(parent,name,f)
> {
> m_windowType = beMDIWindow::Console;
>
> KviIrcNetwork * net = g_pIrcServerDataBase->currentNetwork();
>
> if(net)m_pSrv = net->currentServer();
> QString srvname = m_pSrv->hostname();
> setCaption(srvname);
> connect ( this, SIGNAL( userInput(const QString &) ), this, SLOT ( send(const
> QString &) ) );
> }
>
> beConsole::~beConsole()
> {
> delete m_pSrv;
> }
>
> bool beConsole::connectToServer()
> {
> if(!m_pSrv) { return false; }
> // QString text = "Connect to server";
> // m_pLineView->addLine(text);
> // \n for new liens
> // \t for tabs
> append("<mytag>Connecting to server...</mytag>");
>
> m_pQSocket = new QSocket(this);
> connect( m_pQSocket, SIGNAL( hostFound() ), this, SLOT( hostFound() ) );
> connect( m_pQSocket, SIGNAL( connected() ), this, SLOT( connected() ) );
> connect( m_pQSocket, SIGNAL( readyRead() ), this, SLOT(socketReadyRead()) );
> connect( m_pQSocket, SIGNAL( connectionClosed() ), this,
> SLOT( disconnected() ));
> m_pQSocket->connectToHost( m_pSrv->hostname(), m_pSrv->port() );
> return true;
> }
>
>
> void beConsole::hostFound()
> {
> append("<mytag>Host found ...</mytag>");
> }
>
> void beConsole::connected()
> {
> append("<mytag>Connected to "+m_pSrv->hostname()+"</mytag>");
> QString cmd;
> cmd = "USER alex 0 "+m_pSrv->hostname()+" :Using beIRC 0.0.1\n";
> m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
> cmd = "NICK blackeagle654\n";
> m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
> }
>
> void beConsole::socketReadyRead()
> {
> // append("<mytag>Socket is ready to read</mytag>");
> while ( m_pQSocket->canReadLine() ) {
>
> QString msg = m_pQSocket->readLine();
> QRegExp rxA("(.*)\n$");
> rxA.search( msg );
> append( rxA.cap(1) );
> /* BEGIN ping pong fast reply */
> QRegExp rx("^PING :([0-9]+)");
> int pos;
> if( (pos = rx.search(msg)) != -1)
> {
> QString cmd = "PONG :"+rx.cap(1)+"\n";
> m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
> }
> /* END ping pong fast reply */
> QRegExp rx2("376.*End of /MOTD command");
> if( (pos = rx2.search(msg)) != -1)
> {
> QString cmd = "JOIN #gentoo\n";
> m_pQSocket->writeBlock( cmd.latin1(), cmd.length() );
> }
> }
> }
>
> void beConsole::disconnected()
> {
> append("<mytag>Disconnected from server</mytag>");
> }
>
> void beConsole::send(const QString & command)
> {
> append("<command>"+command+"</command>");
> // command.append("\n");
> QString tmp = command+"\n";
> m_pQSocket->writeBlock( tmp.latin1(), tmp.length() );
> }
>
> KviIrcServer* beConsole::server()
> {
> qDebug("returning server");
> return m_pSrv;
> }
>
>
> Thanks for any help !
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://productguide.itmanagersjournal.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|