Solved - Segementation Fault
-
Hello,
My program is getting a segmentation fault (SIGSEGV signal from Windows 7) while reading a file. The file is an ASCII file with 8380232 lines of text (238 MBytes in size). Each line of text consists of two real numbers seperated by a space. The code below basicially reads and parses each line, converts the strings to doubles and stores the double values in two arrays. I'm stepping through the code with a debugger and all seems well. So, I hit continue and at line 564 (two out of three times, other time it bombed at line 3) of the input text file I get this signal/error. I looked at the data file and nothing unusual is happening there. The code is below along with what was in the compile output window. Does anyone have any knowledge of what might be happening to cause this?
Thanks you.
@
// N8241A::updatewaveforms -> read waveform file & update N8241A AWG waveforms
void N8241A::updatewaveforms(QString filename)
{
QFile file;
QString qline;
int index;
int count;file.setFileName (filename); if ( file.open(QIODevice::ReadOnly) ) { // file opened successfully QTextStream t( &file ); // use a text stream // until end of file... count = 0; while ( !t.atEnd() ) { // read and parse the command line qline = (t.readLine()).trimmed(); // line of text excluding '\n' index = qline.indexOf(" "); ch1_testvector[count] = (qline.mid(0,index)).toDouble(); ch2_testvector[count] = (qline.mid(index+1)).toDouble(); count += 1; } // Close the file file.close(); } else { appendPlainText("N8241A initialization: Unable to open test vector file \n >"); return; }
@
@
Debugging starts
Critical error detected c0000374
(Internal error: pc 0x1 in read in psymtab, but not in symtab.)
(Internal error: pc 0x1 in read in psymtab, but not in symtab.)
(Internal error: pc 0x1 in read in psymtab, but not in symtab.)
(Internal error: pc 0x1 in read in psymtab, but not in symtab.)
(Internal error: pc 0x0 in read in psymtab, but not in symtab.)
(Internal error: pc 0x1 in read in psymtab, but not in symtab.)
(Internal error: pc 0x1 in read in psymtab, but not in symtab.)
(Internal error: pc 0x1 in read in psymtab, but not in symtab.)
(Internal error: pc 0x1 in read in psymtab, but not in symtab.)
(Internal error: pc 0x0 in read in psymtab, but not in symtab.)
Debugging has finishedDebugging starts
Debugging has finished
@ -
Why not use a QVector or QList for the ch1_testvector and use ::append? Maybe your count exceeds the bounds of those arrays.
-
Thanks for replying. I'll look into QVector and QLIst. I'm new to QT. I've sort of grown accustomed to and spoiled by .net/managed code, C++/CLI and C#. I stare at my C++ code here and often don't even see memory bounds issues since I rarely have to concern myself with these things with a memory manager and managed pointers in the Microsoft world.
Well, meanwhile here's my array declarations and instantiations. There should be enough room for the data.
@
double* ch1_testvector;
double* ch2_testvector;
.
.
.
ch1_testvector = new double [8388608];
ch2_testvector = new double [8388608];
.
.
.
@ -
By the way, I think I figured out what was happening. I don't think my class was getting instantiated. I didn't know non-static funcitons of a class would even begin to run without the class being instantiated.
-
Hi, indeed as mentioned before it is a good idea to use Qt storage classes. Then you don't have to think about clearing memory when done with the data. In your case you should! When it is just double you need you can use the QList<double> myList and append any new ones. This is also valid for pointers (QList<double*> myList).
Happy coding! -
When you use the debugger always check for pointer to an object before you are calling some method using that pointer.
And also always use RAII idiom. It helps a lot.