LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2006, 10:28 PM   #1
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
Encapsulation problem (Seg fault at hook)


Hi,
I was trying to encapsulate 4 different kinds of objects into one "master" object. But ended up with "Segmentation Fault" at hook (kernel code)

============================================ CODE
rmves.cpp

#include <iostream>
#include "rmves.h"
#include "rs232.h"


RS232_Controller *rs232;


// Attach an observer to the subject
// so that subsequent interrupts will trigger the db
void SubjectEx::Attach( Observer *o )
{
m_Observer->push_back(o);
}

// Detach an observer to the subject when
// it is no longer used
void SubjectEx:etach( Observer *o )
{
m_Observer->remove(o);
}

// Signals all observers
void SubjectEx::Notify (void)
{
list<Observer *> i;

for (list<Observer *>::iterator iter = i.begin(); iter != i.end(); iter++)
{
i.front()->Update(this);
}
}

....

// Constructor for NOX
NOX::NOX(RS232_Controller *s)
{
m_oSubject = s;
m_oSubject->Attach(this); //<<<<<<<<<<<<<<<< Error!
}

// Destructor for NOX
NOX::~NOX()
{
m_oSubject->Detach(this);
}

void NOX::Update (SubjectEx *theChangedSubject)
{
}

....

int main (int argc, char *argv[])
{
bool res;
pthread_t tid;

rs232 = new RS232_Controller();
res = rs232->CreateThread();
rs232->Join(rs232->getThread(0));
return 0;
}

=================================================
rmves.h

#ifndef __RMVES__
#define __RMVES__
/******************************
Version 2.0
Purpose: define the most basic
type of objects
******************************/
#include <list>
//#include "rs232.h"

#define TRUE 1
#define FALSE 0

#define CARRIAGE_RETURN 13
#define NO_ITERATIONS 100
#define BLACK 0
#define WHITE 255

using namespace std;

class SubjectEx;
class RS232_Controller;
//=======================================
// Class: Observer
// Purpose: Updating Subject
// after receiving signals
// from the sensors
//=======================================
class Observer
{
public:
virtual ~Observer() { }
virtual void Update (SubjectEx *theChangedSubject) = 0;
protected:
Observer() { }
};

//=======================================
// Class: Subject
// Purpose: Monitoring the RS232
//=======================================
class SubjectEx {
public:
virtual ~SubjectEx() { }

virtual void Attach (Observer *);
virtual void Detach (Observer *);
virtual void Notify (void);
protected:
SubjectEx() { }
private:
list<Observer *>*m_Observer;
};

//=======================================
// Class: NOX
// Purpose: class that represent the NOX
// gas reading
//=======================================
class NOX : public Observer {
public:
NOX(RS232_Controller *);
~NOX();
virtual void Update (SubjectEx *);

// Get/Set functions
void SetNOX (double _NOX) { m_fNOXReading = _NOX; }
double GetNOX() { return m_fNOXReading; }

private:
double m_fNOXReading;
RS232_Controller *m_oSubject;
};

======================================================
rs232.h

#ifndef __RS232__
#define __RS232__
/**************************
Version 2.0
Purpose: Defines RS232
Controller
***************************/
#include "rmves.h"

#define MAX_THREADS 10

//=========================================
// Class: RS232_Controller
// Purpose: Controlling the RS232 interface
//=========================================
class RS232_Controller : public SubjectEx
{
public:
bool CreateThread(void);
// Join the thread pool
void Join (pthread_t pthread);

// Thunk that wraps C Thread Objects
void *PortMonitorThread(void*);

pthread_t getThread (int index) { return m_Threads[index]; }
public:
RS232_Controller() { m_oNOX = new NOX();
m_oHC = new HC();
m_CO = new CO();
m_CO2 = new CO2(); }
~RS232_Controller() { }
private:
NOX *m_oNOX; //(changed) <<<<<<<<<<<< Encapsulation
HC *m_oHC; // (changed)
CO *m_oCO; // (changed)
CO2 *m_oCO2; // (changed)
private:
pthread_t m_Threads[MAX_THREADS];
int m_iNumThreadsRunning;
};
#endif

===================================================
And this part...
void *RS232_Controller::PortMonitorThread (void *a)
{
struct termios oldtio, newtio;
struct sigaction saio;
unsigned char *buff;
size_t bytesread, totalbytesread;
WebCam *webcam1;
MyPacket *packet;
PlumeData *plumedata;

fileHandle = open (LINUX_SERIAL_DEVICE, O_RDWR | O_NOCTTY); // | O_NONBLOCKq);
if (fileHandle == 0)
{
fprintf (stderr, "COM1 is not connected\n");
exit(-1);
}



saio.sa_handler = signal_handler_IO;
//saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction (SIGIO, &saio, NULL);


fcntl ( fileHandle, F_SETOWN, getpid() );

// set to the file for nonblocking read
fcntl ( fileHandle, F_SETFL, FASYNC );

// clear attributes
bzero (&newtio, sizeof(newtio));

// save old attributes
tcgetattr ( fileHandle, &oldtio );

// control flags
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

// input Flag
newtio.c_lflag = IGNPAR | ICRNL;

// output flag
newtio.c_oflag = 0;
newtio.c_lflag = ISIG;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcflush ( fileHandle, TCIFLUSH );

tcsetattr ( fileHandle, TCSANOW, &newtio );

buff = (unsigned char *) malloc (65535);
memset ( buff, 0, 65535 );

webcam1 = new WebCam("192.168.0.124");

while (STOP == false)
{
packet = new MyPacket(16);
// chk for rece buff first
do {
bytesread = 0;
printf ("packet->GetPacketSize() = %d\n", packet->GetPacketSize());
bytesread = read ( fileHandle, buff, packet->GetPacketSize()); // <<<<<<<<<<<<<<<<<<<< Can't get thru this blocking read
printf ("bytesread = %d\n", bytesread);
totalbytesread += bytesread;
} while (totalbytesread != strlen ((const char *)buff));

===================================================
Thanks
Jack

Last edited by lucky6969b; 03-16-2006 at 02:12 AM.
 
Old 03-15-2006, 10:58 PM   #2
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
The spaces between tokens were deleted. So you see really bad indentation.
Sorry about that.
Jack
 
Old 03-15-2006, 10:59 PM   #3
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
Or automatically deleted! not my fault
 
Old 03-16-2006, 07:29 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by lucky6969b
The spaces between tokens were deleted. So you see really bad indentation.
Sorry about that.
Jack
thats because you dont use code tags..

how about an edit and put in the tags..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't login - seg fault dbc001 Slackware 8 05-17-2006 02:42 PM
RH 7.2 crash...getting seg fault when using ls sakima Red Hat 1 10-11-2004 09:14 AM
C seg fault drigz Programming 5 10-01-2004 03:35 PM
Openoffice Seg. Fault Problem solspin Linux - Software 0 09-01-2003 11:38 PM
xawtv seg fault lackluster Linux - Hardware 8 08-18-2003 12:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration