Thread: Murderous function...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    21

    Murderous function...

    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, "&#37;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???
    Last edited by CornedBee; 08-15-2007 at 01:48 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you have a debugger? Can you see the call stack when the crash occurs?

    If you don't have a debugger, maybe add a return; line to make the function return early. Start with it near the top and then move it down until you find the place where the crash first occurs.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    21
    So, it turns out to be that lovely sprintf statement, converting the area under the curve to a string to paste into the filename...Oh well, I'll just put the area into the actual file. Thanks for your help

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    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';
    ...
    
    cout << "Computed area = " << areaTotal << endl;
    char area[10];
    for(int i = 0; i < 9; i++)
        area[i] = ' ';
    sprintf(area, "&#37;f", areaTotal);
    area[9] = '\0';
    Are you absolutely certain that currentWindow and drvLVL2 and areaTotal will all fit into 4/5/10 characters when converted (including nulls)? What are the typical values for these variables?

    You can get rid of all that conversion code using a single stringstream:
    Code:
    stringstream filename;
    filename << "data-" << currentWindow << '-' << drvLVL2 << "area" << areaTotal << ".xls";
    ...
    cout << "Creating instance of ofstream..." << endl;
    cout << "Actual file name = " << filename.str() << endl;
    system("PAUSE");
    ofstream file(filename.str().c_str());
    Last edited by hk_mp5kpdw; 08-15-2007 at 01:27 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    21
    I'll try that, thankyou. But yes, I am absolutely sure the size of those variables isnt to large.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    21
    Wow, that works beautifully; no more C functions...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM