Hello,

I'm sorry for the bad title for this, but I honestly have no idea how to define this by few words.

My problem is this, I need to read a bunch of data from a text file into an array, in the theoratical aspect, it's all nice and should work, compiler seems to fancy it as well, but when it comes down to actuallity, it just bails.

This is the code:

int rep2 = 0;
for(int y = 0; y < map._m->yTile; y+=16)
   for(int x = 0; x < map._m->xTile; x+=16){
      int k = atoi(this->loadSeek("Maps", "Solid", str, rep, rep2).c_str());
      map._m->getTile(x/16, y/16).solid = k;
      rep2++;
}

loadSeek() is a function that takes the category and value strings in the first 2 arguements and searches for their saved values in the string in the 3rd. rep and rep2 are part of an overloading of that function that makes it capable for searching for data inside a particular item inside a value, and return all sub-values that are inside a certain value, respectively.

The code for this loadSeek() is this:

string Project::loadSeek(string category, string value, string str, 
   int rep, int rep2){
   int pStart, pEnd;
   category.insert(0,"~");
   category.insert(category.length(),"{");
   value.insert(value.length(),":");
   pStart = str.find(category);
   for(int i = 0; i < rep; i++)
      pStart = str.find("<", pStart)+1;
   pStart = str.find(value, pStart);
   pStart = str.find("[", pStart)+1;
   for(int i = 0; i < rep2; i++)
      pStart = str.find(", ", pStart)+2;
   pEnd = str.find(",", pStart)-pStart;
   return str.substr(pStart, pEnd);
}

This code isn't really relevant, as I already confirmed it to be working, so I'm not going to get into it much.

The getTile() function is a function of the map class, that returns the array position of the tile from a matrix of tiles for that map, as it needed to be dynamically declared, it had to be done in a 1 dimensional array, and so this function is used to access the members in a 2 dimensional manner.
This is the function:

Tile &Map::getTile(int x, int y){
   return this->tile[(16*y*this->xTile)+(x*16)];
}

This code is also confirmed working.

So theoratically, the code

int rep2 = 0;
for(int y = 0; y < map._m->yTile; y+=16)
   for(int x = 0; x < map._m->xTile; x+=16){
      int k = atoi(this->loadSeek("Maps", "Solid", str, rep, rep2).c_str());
      map._m->getTile(x/16, y/16).solid = k;
      rep2++;
}

needs to work.

I've been tracking it through debug and got this error:
"This may be due to corruption of the heap, which indicates a bug in CLRTest.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while CLRTest.exe has focus.

The output window may have more diagnostic information."

All the output window shows is a list of DLLs that have been loaded, like it should, nothing special.
Debugging would show that the next line to be executed should be the one with the seekLoad().


When I try to run the program regularly, not in debug, the error would be different:
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."


Now, the weird thing is, I used to have both the lines for getting the tile I'm working on with getTile() and getting the data with seekLoad in one line without k to bring them together, and it always gave these errors I mentioned above, that is why I tried splitting them to what you see now, they still did the same errors.
However, if I tried to remove either one of them from the code, the other would work smoothly, just getting the data and putting it into k would work if it had not to be drawn into the tile. And the tile would take a static value of 0 if it hadn't been asked to take k in.

Now, I really don't know what to make of this now, I obviously need both functions in that code, yet I'm clueless as to why they wouldn't work together, yet alone they would...

Has anyone any input on this?
I would much aprreciate any help.

Thanks.

I would like to know what this->tile is. Why 16?
Without knowing this I would guess that there is something wrong with getTile.

What compiler/debugger do you use? A backtrace would be very helpful. A backtrace would tell you which part of your code is causing the error.

this->tile is a member of the Map class, which is a dynamic array, it multiplies by 16 because the function takes the coordinates by tile(Which is 16x16 pixels) and not by pixel, yet the array itself contains all the pixels also, to save the trouble of having another dynamic array inside a dynamic array to handle specific pixels.

So I multiply it by 16 to access the first cell of each tile which contains the info that I need, this is known and is working.

I'm using Visual Studio 2k8 with its' built in debugger.
Backtracing gives me nothing, like I said, the code that causes the error is this part:

int k = atoi(this->loadSeek("Maps", "Solid", str, rep, rep2).c_str());      
map._m->getTile(x/16, y/16).solid = k;

Both lines work on their own if the other is removed from the code, and both functions used are not making any of the problems themselves, in fact, when debugging, the crush would always come at a different location and time of running, but always during the procession of these 2 lines.

It's been a few days since I started dealing with this issue now, and have found no solution so far. Any help at all would be appreciated here.

When you say "Both lines work on their own if the other is removed from the code, and both functions used are not making any of the problems themselves," I am skeptical.

Usually, if a program crashes when doing A and B, and it seems to work when doing A alone or B alone, what it really means is that there is a problem in A, B, or both, but the problem isn't visible until you happen to run both of them together.

I think I would start looking for the problem by putting extra code everywhere there is a computed array reference (for example, in getTile) to verify that the index you compute is actually within the bounds of the array. Who knows? You might get a surprise.

Ok, I see.
To give you a better answer I would like to see a backtrace of the stack frames.

Ok, I see.
To give you a better answer I would like to see a backtrace of the stack frames.

Right. And... how exactly do I do that? Sorry =\

And Arkoenig, thanks, I'll see if I can do anything that could shed light on this, however, to that, I am skeptical, as I've already tried following the breakpoints I set multipile times, and I'm not quite sure how this could help, but I will try. Thanks.

Edit:
This is weird...
I tried running it as it was one more time before trying to add more code to check, when I seperated the 2 lines this time, the one with getTile() would crash the program every time.
I haven't changed anything, I have no idea why it happens now and hadn't before, but as with last times, the values are supposed to be correct, it's supposed to be working.

It seemingly crashes when it tries to get the value at getTile(0, 1).
getTile(0, 1) would mean tile[(16*1*320)+(0*16)] = tile[5120], with the image I'm working on, tile should be an array of 51200 cells, it should certainly be in range, I still have no idea what makes it crash.

I think it is Debug -> Windows -> Call stack.

I actually think it won't be necassery, I think I just realized what I did wrong, and if I'm right, I'm just plain dumb... Give me a few minutes...

Edit:

Yea.. I got it...
The reason it was crushing wasn't because the functions were wrong, it was because the data was.
When I was first making the save function for the data I told it to parse a line(\r\n) every time a line of pixels on the picture had ended. Time came by, and I just forgot about doing that, the loading function disregarded that hole completely, and that was why it always crashed on the transition between the last picture of a line to the first of the next, that was when it crashed though, I'm still unsure why it sometimes worked fine...

Either way, that is the reason for this, as stupid as it may be... Sorry for wasting your time guys, but I appreciate the efforts.

It seemingly crashes when it tries to get the value at getTile(0, 1).
getTile(0, 1) would mean tile[(16*1*320)+(0*16)] = tile[5120], with the image I'm working on, tile should be an array of 51200 cells, it should certainly be in range, I still have no idea what makes it crash.

When you say "it should certainly be in range," that says to me that you do not actually know whether it is in range. That's why I'm suggesting you modify getTile so that it does a range check.

Just updating 1 last time...

After working a little farther along and realizing that the problem persisted, I realized it wasn't only the string that was corrupt, as the program was crushing again.

I started looking again, and then I realized that the reason it actually did crash was because of the way the map._m class was constructed... the class was instantiated outside of the loop as MMap map(0, 0), when the parameters refer to the width and height of the map, they were to be changed to their correct values later when those would be read by the function above in this thread, and they were.

What I didn't take into account though, was that the constructor of the map class was what constructed the array used in getTile, which resulted in a class of size 0*0 instead of the later x*y, and hence the problem and the reason that it complained about being out of range.

To fix this, I made the constructor go inside the loop as

MMap map(atoi(this->loadSeek("Maps", "X", str, rep).c_str()), 
atoi(this->loadSeek("Maps", "Y", str, rep).c_str()));

And that permanentaly solved it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.