Sign in to follow this  

vector / string issues

This topic is 4158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i was holding off on posting this because i thought i had an older version that worked, but it turns out that too has the same issue. description of the problem: i have a working .3ds loader. it takes in the model data correctly and places it into my storage object correctly. i also thought i had a working texture loading method. i say thought because up until today it worked just fine, and now it doesn't. i have one file that only has one texture to load and it works fine. i have another file that loads 4 textures and that now breaks. previously it had worked. the program also breaks when i try files with 6 and 8. it is apparently breaking after loading the 2nd texture. the only difference between the previously working method and this is ... nothing. absolutely nothing changed for the code that loads textures. the only difference between the two total projects is in the most recent i changed a struct to a class, but that data type is never used by the texture loading algorithm or even in the data that holds the overall .3ds info. the reason i say the string variables are disappearing is because the values within the variables becomes nothing, even going so far as to cause a seg fault which even means they are pointing to data that doesn't belong to them. however, this doesn't make sense because prior to trying to load the actual image files into memory i can get those string values (which are the material name and the texture filename).
  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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Are you sure testobj.materials is at least as big as testobj.nummaterials?

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by supercat1
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!


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
[edit]:

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]

Share this post


Link to post
Share on other sites

This topic is 4158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this