gdb control jumping to random position

Here is the code snippet which I want to debug: When I try to debug using GDB, after the loop gets over for i = 0, it does not jump to the very first line inside the loop for i = 1. Instead it jumps to another line in the middle and then jumps to the first line. I am not able to figure out why and how to fix it. Please give me your inputs.

for(i = 0; i < fileSum; i++)
    char *fileName ; //= NULL;
//  fileName = GetFileName(dataset->groundTruthFolder, trainImageFiles[i], dataset->groundTruthExtension);

    fileName = new char[strlen(dataset->groundTruthFolder) + strlen(trainImageFiles[i]) + strlen(dataset->groundTruthExtension) + 1];
    sprintf(fileName, "%s%s%s", dataset->groundTruthFolder, trainImageFiles[i], dataset->groundTruthExtension);
    printf("GetFileName: fileName-%s \n", fileName);

    LLabelImage groundTruth(fileName, domain);
//      delete[] fileName;
//      fileName = NULL;

    int width = groundTruth.GetWidth(), height = groundTruth.GetHeight();

    int subWidth = (width + subSample - 1) / subSample;
    int subHeight = (height + subSample - 1) / subSample;

    for(k = 0; k < subHeight; k++) for(j = 0; j < subWidth; j++)
        unsigned char gtVal = groundTruth(j * subSample, k * subSample, 0);
        if(gtVal > 0) classCounts[gtVal - 1]++, total++;
        printf("k = %d,j = %d, gtVal = %d\n", k, j, gtVal);
    int dummy = 0; 
    printf("i = %d\n", i);

Here is the gdb console's snapshot:

Breakpoint 2, LDenseUnaryPixelPotential::InitTrainData (this=0x80aad08, trainImageFiles=...) at potential.cpp:665
665         int dummy = 0; 
(gdb) n
666         printf("i = %d\n", i); 
(gdb) n
i = 1
650         LLabelImage groundTruth(fileName, domain);
(gdb) n
640     for(i = 0; i < fileSum; i++)
(gdb) n

Breakpoint 1, LDenseUnaryPixelPotential::InitTrainData (this=0x80aad08, trainImageFiles=...) at potential.cpp:645
645         fileName = new char[strlen(dataset->groundTruthFolder) + strlen(trainImageFiles[i]) + strlen(dataset->groundTruthExtension) + 1];

Please notice that, the control goes from 666 to 650 and then to 640, instead of going directly from 666 to 640.

Previously, I was compiling with -g option , now I compiled with -g -O0 option. Still the same problem persists.

Here is my Makefile:

CC  =   g++
OBJS    =   main.o
SOURCES =   main.cpp
LIBS    =       -lIL -pthread
EXE =   ale
FLAGS   = -g -O0 -Wno-write-strings

$(EXE): .
    $(CC) $(FLAGS) -c $(SOURCES) -o $(OBJS)
    $(CC) -o $(EXE) $(OBJS) $(LIBS)
    rm -rf *.o ale


Actually, I debugged further and found that when the control reaches the end of the loop, the destructor of "LLabelImage" is being invoked to delete the data.

Need Your Help one method per time restriction, or async method call blocking another methods


I have a rather long-running background task that I call with from frontend and then through called server-side method. This task returns a value eventually. Application don't really ...

how to get the key value from the nested hash inside the array?

ruby-on-rails arrays hash

I have a array which is inside a hash. I want know the result of the student (pass/fail) using the following array. First I have to match them with particular standard and compare their marks with ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.