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

Array.Sort in with nontrivial comparison function

c# algorithm sorting lambda

Consider the following code from C# 5.0 in a Nutshell, p. 289:

ocaml - array of record with mutable field

ocaml mutable records

I'm new to OCaml and i'm trying to understand the concept of mutable record field.

JQuery and Javascript doesn't work in my project, but on web does

javascript jquery html jquery-ui

I reused a code that I saw on StackOverFlow with my data : , but when I'm importing him in Visual Studio 10 the buttons doesn't work.

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.