PDA

View Full Version : Program error while executing, ok while debugging



harmodrew
11th August 2010, 01:04
Hello!
My program behaves in a very strange way! When I run it in debug step-by-step mode all goes good but when I run it in "normal" mode it raises a segmentation error around a dynamically allocated vector! As I said this error doesn't come up while debugging...

Here is the link to the project (I've uploaded it to mediafire) http://www.mediafire.com/?vbwre2a73vl8ju9

This program simply has to find a path between two nodes of a graph.
Starting from the node 0 (first row of the adjacency matrix) it inspects all the nodes searching for the first uninspected node which is the end node (fourth row).

I've posted a light version of the project with only the necessary to raise up the segmentation fault (infact it is a fixed graph, etc...)

Ask me if you need more info :)

thanks a lot

norobro
11th August 2010, 04:37
Not much Qt code in your project is there?

Don't know if these are causing your seg fault or not but here are two things that I noticed: realloc() can move the memory block so you need to return the pointer to the calling function.
check your deletes

harmodrew
11th August 2010, 10:13
realloc() can move the memory block so you need to return the pointer to the calling function.

here I use realloc()...what's wrong?



void AddInspectedNode(int idxNode, int *Inspected, int *nInspected)
{
(*nInspected)++;
Inspected = (int *) realloc(Inspected, (*nInspected) * sizeof(int));
Inspected[(*nInspected)-1]= idxNode;
}



check your deletes

I've changed the Graph destructor into:


Graph::~Graph()
{
for (int i = 0; i<nNodes;i++)
delete [] mat[i]; //THIS CHANGED
delete [] mat;
}


and the CheckPath method of Graph:


int Graph::CheckPath(int idxEntry, int idxExit)
{
int result, nInspected, *Inspected, i;

result = nInspected=0;
Inspected = new int[1];

PathExists(idxEntry, idxExit, mat, nNodes, Inspected, &nInspected, &result);

delete [] Inspected;

//DON'T NEED TO DEALLOCATE HERE THE MATRIX
/* for (i=0; i<nNodes; i++)
delete [] mat[i];
delete [] mat;
*/
return result;
}


but still get the error...:(