Ok, so I have this program that works with a spectrometer (reads in spectra) and an SLM (spatial light modulator). Everything works great except for one particular function. The function that doesn't work is called from another function and should simply read in the current values of the spectrum and write them to a file. In fact, it DOES do that. When I call the function everything works fine, the file is created, contains the correct data, etc; however, when the function returns the program crashes. I know the code of the function works properly because I've ported it to its own program and that works beautifully. I imagine the problem lies in the releasing of memory allocated during the execution of the function but I have no idea how to resolve that...
Heres the code where the function is called:
Code:
if(Saveflag == 'y')
{
cout << "Attempting to save spectrum..." << endl;
SaveCurrentSpectrum(currentWindow, drvLVL2);
}
Heres the function definition:
Code:
//Cout statements are for debugging purposes
void SaveCurrentSpectrum(int currentWindow, int drvLVL2)
{
SpectrometerFactory factory;
ArrayOfObjects spectrometers = factory.getAllSpectrometers();
char win[4];
for(int i = 0; i < 3; i++)
{
win[i] = ' ';
}
win[3] = '\0';
char lvl[5];
for(int i = 0; i < 4; i++)
{
lvl[i] = ' ';
}
lvl[4] = '\0';
itoa(currentWindow, win, 10);
win[3] = '\0';
itoa(drvLVL2, lvl, 10);
lvl[4] = '\0';
Spectrometer** specs = (Spectrometer**)spectrometers.getObjects();
ArrayOfObjects chArray = specs[0]->getChannels(); //Gets the channels from first spectrometer
cout << "Creating array of spectrometer channels gathered from array of objects..." << endl;
SpectrometerChannel** channelArray = (SpectrometerChannel**)chArray.getObjects();//Creates array of channels for spectrometer
cout << "Creating spectral processor for index zero of channelarray..." << endl;
SpectralProcessor processor(*channelArray[0], 0);//Creates instance of spectral processor which will aquire spectrum
cout << "Setting processor integration time..." << endl;
processor.setIntegrationTime(5000);
cout << "Correcting for processor electrical dark..." << endl;
processor.setCorrectForElectricalDark(1);
cout << "Setting processor smoothing window size..." << endl;
processor.setSmoothingWindowSize(0);
cout << "Creating pixels..." << endl;
int pixels = processor.getNumberOfPixels();
int dark = processor.getNumberOfDarkPixels();
cout << "Creating spectrum instance by getting unfilled spectrum from the processor..." << endl;
Spectrum rawspectrum = processor.getUnfilledSpectrum();//Gets new instance of spectrum from the processor
cout << "Creating specrtum data by processing rawspectrum..." << endl;
Spectrum specData = processor.getSpectrum(rawspectrum);//Gets spectral data
cout << "Creating doublearray of spectrum data..." << endl;
DoubleArray data = specData.getSpectrum();//Gets intensity data from the spectrum
double *Ydata = data.getDoubleValues(); //Intensity
cout << "Gettint wavelength data from the first channel in the array..." << endl;
DoubleArray wvlengths = channelArray[0]->getAllWavelengths(); //Gets wavelengths from spectrometer
double *Xdata = wvlengths.getDoubleValues();//Wavelengths
double dx = 0;
double dy = 0;
double areaTotal = 0;
double areaPoint = 0;
cout << "Calculating area under curve..." << endl;
for(int i = 0; i < (pixels - 1); i++)
{
dx = Xdata[i + 1] - Xdata[i];
dy = Ydata[i] + Ydata[i + 1];
areaPoint = ((dx * dy)/2);
areaTotal += areaPoint;
}
cout << "Computed area = " << areaTotal << endl;
char area[10];
for(int i = 0; i < 9; i++)
area[i] = ' ';
sprintf(area, "%f", areaTotal);
area[9] = '\0';
system("PAUSE");
/*****************************************************/
cout << "Writing Data to file..." << endl;
string filename = "data-";
filename += win;
cout << "win = " << win << endl;
filename += "-";
filename += lvl;
cout << "Lvl = " << lvl << endl;
filename += "area";
filename += area;
filename += ".xls";
cout << "Creating instance of ofstream..." << endl;
cout << "Actual file name = " << filename << endl;
system("PAUSE");
ofstream file(filename.c_str());
cout << "Data length = " << data.getLength() << endl;
cout << "Wavelengths length = " << wvlengths.getLength() << endl;
cout << "Pixels length = " << pixels << endl;
cout << "Actually sending data to xls file..." << endl;
for(int count = 0; count < pixels; count++)
{
file.precision(8);
file << Xdata[count] << '\t' << Ydata[count] << endl;
}
cout << "The file saves, but..." << endl;
file.close();
/*****************************************************/
system("PAUSE");
}//BOOM
Sorry its garbled, the little tiny text box on here doesnt want to fit my code...
Or the whitespace is just really screwed up. There, fixed it for you. -- CornedBee
It prints alot to the screen that isnt necessary. The function crashes precisely after that last system("PAUSE"). Any ideas???