This is a C++ issue rather than a QT issue in my view hence the post here.

When I run the functions below I get

"Program received signal SIGABRT, Aborted.
free(): invalid next size (fast): 0x083101b0 ***"
plus a hundred or so lines of related errors.

I believe I am deleting an invalid pointer but cannot see where I am going wrong. What I'm trying to do is write two functions that gathers the non empty and non txt containing cells from my spreadsheet selected range then sums the numbers and puts the answer in the cell below the selected range. If there is only one number the functions work, if the range has two or more cells than I get the crash above. Can anyone help sort this out?

Qt Code:
  1. void Spreadsheet::sumColumn() // sums selected cells and dumps answer below lowest cell
  2. {
  3. QTableWidgetSelectionRange range = selectedRange(); // get selected range
  4. int col=currentColumn(); //get current column position
  5. int rowcount= range.rowCount(); // work out number of rows selected
  6. int *number= new int; // create pointer to number of items to be collected
  7. double *dataSet = new double; // create pointer for array of data to be collected
  8. double total=0; // initialise total
  9. collectData(number, dataSet); // get data from sheet see next function
  10. int numberOfItems=0; // set this to zero just in case
  11. numberOfItems=(int) *number; // cast to make numberOfItems = number of filled cells from collect function
  12.  
  13. for (int n=0; n<numberOfItems; n++)
  14.  
  15. total= total+ dataSet[n]; // calculate sum
  16.  
  17. QString num = QString::number(total); // converts any number type to string -very easy!
  18.  
  19. setFormula(rowcount, col, num); // put sum on sheet in cell below selected range this works
  20.  
  21. // in future preferably needs check for overwriting of cell if not empty
  22. // also needs to add row if all rows in sheet are selected
  23.  
  24. delete number; // clean up memory
  25. delete dataSet;
  26. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. void Spreadsheet::collectData(int *count, double *data)
  2. {
  3.  
  4. QList<QTableWidgetItem *> items = selectedItems();
  5.  
  6. int totalcount=0;
  7.  
  8. foreach (QTableWidgetItem *item, items) // loop through sheet selected items
  9. {
  10. bool ok;
  11. double value = item->text().toDouble(&ok);
  12.  
  13. if (ok && !item->text().isEmpty()) // ignore empty cells and cells with anything but 0-9. in
  14. {
  15. //data[totalcount]=0.0; // set each element to zero just in case
  16. data[totalcount]=value; // now fill array with values from sheet
  17. totalcount++; //increment count for only filled cells with numbers in
  18. }
  19.  
  20. }
  21. *count=totalcount; // set pointer to total count
  22. }
To copy to clipboard, switch view to plain text mode