# vector / string issues

## Recommended Posts

  for(int i = 0; i < testobj.nummaterials; i++)
{
cout << "i = " << i << endl;
testobj.materials[i].materialtexture.textureidnum = -1;
if(testobj.materials[i].materialtexture.filename.size() > 0)
{
cout << "materialtexture name: " << testobj.materials[i].materialtexture.filename << endl;
ilGenImages(1, &ImageName);
ilBindImage(ImageName);
if(ilLoadImage(const_cast<char*> (testobj.materials[i].materialtexture.filename.c_str())) == IL_FALSE)
{
cout << "unable to open: " << testobj.materials[i].materialtexture.filename << endl;
}
else
{
//        cout << "i = " << i << "\ttextobj.materials[i].texture.texturidnum = " << testobj.materials[i].texture.textureidnum << endl;
cout << "able to load: " << testobj.materials[i].materialtexture.filename << endl;
testobj.materials[i].materialtexture.textureidnum = ilutGLBindTexImage();
testobj.materials[i].materialtexture.texwidth = ilGetInteger(IL_IMAGE_WIDTH);
testobj.materials[i].materialtexture.texheight = ilGetInteger(IL_IMAGE_HEIGHT);
testobj.materials[i].materialtexture.data = ilGetData();
glBindTexture(GL_TEXTURE_2D, testobj.materials[i].materialtexture.textureidnum);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, testobj.materials[i].materialtexture.texwidth, testobj.materials[i].materialtexture.texheight, GL_RGB8, GL_UNSIGNED_BYTE, testobj.materials[i].materialtexture.data);
}
ilDeleteImages(1, &ImageName);
cin >> a;
}
}


i have tried using insight to get the values while in that for loop, but to no avail. does anyone have any idea what might be happening? i'm totally at a loss. thanks! [Edited by - supercat1 on July 28, 2006 10:53:56 PM]

##### Share on other sites
Are you sure testobj.materials is at least as big as testobj.nummaterials?

##### Share on other sites
yes, i have actually changed that line to "testobj.materials.size();" since materials is stl::vector<material_type> (where material_type is one of my data types).

i would also like to say that apparently my "working" version also produces seg faults. it produces them on the .3ds files with 6 and 8 textures. it is crashing on the attempt to load the 3rd texture (so it gets to i = 2 and fails).

i had some thoughts that maybe the data for the strings was being over written somehow, but that wouldn't make sense since the memory should be allocated to it and therefore safe from overwrites, right? also, there are no calls to the destructor for object_type (which is what testobj is an instance of), so it wouldn't be magically deleted in there. when using insight i did notice that for some reason insight would say that the value of "i" would get really big, but that doesn't make sense because the program always was printing the right value and because it should fail the "i < testobj.materials.size();" check if it did get really big.

so i'm still at square one. :-\ anyone got a really great debugger they can suggest? i'm using dev-cpp and i would prefer to have something that isn't tied into any particular ide.

thanks!

##### Share on other sites
Quote:
 Original post by supercat1yes, i have actually changed that line to "testobj.materials.size();" since materials is stl::vector (where material_type is one of my data types).i would also like to say that apparently my "working" version also produces seg faults. it produces them on the .3ds files with 6 and 8 textures. it is crashing on the attempt to load the 3rd texture (so it gets to i = 2 and fails).i had some thoughts that maybe the data for the strings was being over written somehow, but that wouldn't make sense since the memory should be allocated to it and therefore safe from overwrites, right? also, there are no calls to the destructor for object_type (which is what testobj is an instance of), so it wouldn't be magically deleted in there. when using insight i did notice that for some reason insight would say that the value of "i" would get really big, but that doesn't make sense because the program always was printing the right value and because it should fail the "i < testobj.materials.size();" check if it did get really big.so i'm still at square one. :-\ anyone got a really great debugger they can suggest? i'm using dev-cpp and i would prefer to have something that isn't tied into any particular ide.thanks!

Try ckecking the default constructors (etc) of material_type for problems.

##### Share on other sites
well, i know this isn't great practice probably, but it is a struct currently. i have been debating whether or not to change all my structs to classes, and i guess now is a great time to do that. not ideal, but whatever i guess. :-/

but no ideas on debuggers? i would really like to have a good one on hand anyways.

thanks!

##### Share on other sites
Well, the line:

const_cast<char*> (testobj.materials[i].materialtexture.filename.c_str())

DOES scare the hell out of me. Are you sure the function calls performed on this are not doing unholy non-const things with the character array?

##### Share on other sites
I would be more scared by a function that called itself "ilLoadImage" and modified the provided filename, than by the const_cast involved in using it o__O

##### Share on other sites
here is why i'm using const_cast<char*>

i'll try using the temp vector buffer. see if that works. but the thing is, the program will error out before it ever gets to the "ilLoadImage" fuction. i know this because i have put in "cout << testobj.materials[i].materialname << endl << testobj.materials[i].materialtexture.filename << endl;" before the "if(...filname.size() > 0)" check and it has a segfault.

##### Share on other sites
:

well, i figured i would actually go through and debug the "loadTextureFile()" code first. so i am working on it, and right now i have discovered that between the "glTexImage2D" call and the "ilDelete()" the following happens:

- p_filename goes from "METAL FL.TIF" with space for "77" chars and the first being 'M' to "\220#E" with space for "-112" chars with the first being '\220'
- p_texturewidth goes from 256 to 0
- p_textureheight goes from 256 to 4098
- p_textureidnum goes from 1 to 6
- temptexid goes from 2 to 4

this would make sense why the stl_iterator.h file is where insight ends after the segfault -- vectors can't hold negative ammounts of things (obviously) let alone -112. however, i don't understand why stepping out of that nested block of code would cause all of those values to suddenly change to useless and incorrect values. is there something i am missing?

void texture_type::loadTextureFile(){  ILenum Error;  GLenum GLError;  ILuint temptexid;  ILboolean tempsuccess;  ilGenImages(1, &temptexid);    while((Error = ilGetError()) != IL_NO_ERROR)    {      std::cout << "!! -- " << iluErrorString(ilGetError()) << std::endl;    }    ilBindImage(temptexid);    while((Error = ilGetError()) != IL_NO_ERROR)    {      std::cout << "!! -- " << iluErrorString(ilGetError()) << std::endl;    }    tempsuccess = ilLoadImage(const_cast<char*>(p_filename.c_str()));    while((Error = ilGetError()) != IL_NO_ERROR)    {      std::cout << "!! -- " << iluErrorString(ilGetError()) << std::endl;    }    if(tempsuccess)    {      std::cout << "image loaded correctly\n";    }    else    {      std::cout << "image loaded incorrectly\n";    }    if(tempsuccess)  {//    tempsuccess = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);//     // for some reason i was getting segfaults at the end of this function //     // when the above line was left uncommented, i'm loading a .tif file//     // so i don't know if IL_RGB is right or if it should be IL_RGBA//    while((Error = ilGetError()) != IL_NO_ERROR)//    {//      std::cout << "!! -- " << iluErrorString(ilGetError()) << std::endl;//    }//    if(tempsuccess)//    {//      std::cout << "image converted correctly\n";//    }//    else//    {//      std::cout << "image converted incorrectly\n";//    }//    //    if(!tempsuccess)//    {//      // logger info that the file was not of IL_RGB type, probably use //      // ilGetError and iluErrorString() to output to the logger//      ilDeleteImages(1, &temptexid);//      return;//    }    p_texturewidth = ilGetInteger(IL_IMAGE_WIDTH);    p_textureheight = ilGetInteger(IL_IMAGE_HEIGHT);        glGenTextures(1, &p_textureidnum);      while((GLError = glGetError()) != GL_NO_ERROR)      {        std::cout << "!! -- " << gluErrorString(GLError) << std::endl;      }        glBindTexture(GL_TEXTURE_2D, p_textureidnum);      while((GLError = glGetError()) != GL_NO_ERROR)      {        std::cout << "!! -- " << gluErrorString(GLError) << std::endl;      }    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);      while((GLError = glGetError()) != GL_NO_ERROR)      {        std::cout << "!! -- " << gluErrorString(GLError) << std::endl;      }    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);      while((GLError = glGetError()) != GL_NO_ERROR)      {        std::cout << "!! -- " << gluErrorString(GLError) << std::endl;      }    glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());      while((GLError = glGetError()) != GL_NO_ERROR)      {        std::cout << "!! -- " << gluErrorString(GLError) << std::endl;      }  }  else  {    // logger info that the file was not opened, probably use ilGetError and    // iluErrorString to output to the logger, when i get a fully functioning     // logger completed  }    ilDeleteImages(1, &temptexid);    while((Error = ilGetError()) != IL_NO_ERROR)    {      std::cout << "!! -- " << iluErrorString(ilGetError()) << std::endl;    }  // when i used to get segfaults it would be after this, so it must have been  // a constructor somewhere was messing up}

[Edited by - supercat1 on July 29, 2006 1:53:36 AM]

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978702

• 21
• 14
• 12
• 10
• 12