Sign in to follow this  
Marianne

mysterious bug

Recommended Posts

hi, i'm having quite a trouble in a big project in C++ (i've got like 20 files of code so i can't post them all, just tell me if you need to see more of them) i have a vector filled with Texture objects. (the users can add textures by clicking on a button) and the texture object holds an int called text_index, holding the ID of the texture. Texture #0 is added auto when the app is launched, and its text_index int contains 0. But the second the user adds another texture, text_index of texture 0 gets corrupt and contains garbage! text_index is private and there is no method that can modify it except the constructor. It only happens to Texture 0. All other texture objects work perfectly. Futhermore, all other variables inside that class are still okay when tex_index get corrupt. I dunno what's wrong with THAT variable in particular! But weird thing, i found out that if i add another int (that i called 'useless_int') the line before i declare tex_index, then everything works perfeclty! (See code below for better explaination) what can that problem be?? that doesn't make any sense [help] Here is the header of Texture:

class Texture {
private:
    
    int useless_int; // if i comment this line out, tex_index gets corrupt!
    int tex_index;
    
...
    
public:
    
    int getID();
...

    // constructor
    T3Texture(wxString title, T3GeomArea* parent, int tex_index_arg);
};





thanks!

Share this post


Link to post
Share on other sites
Does 'useless_int' get garbled? I have had something 'mysterious' like this happen in the past, too. For me, the case was that the compiler recognized that a variable was never used in the code, so it garbled its value. (Something about register re-allocation).

Try:

1) Set the variable to zero in the constructor (if you're not doing that already).
2) Try "using" the value --> 'int dummy = tex[0].tex_index' or something, later on in the program, see if that helps keep the value from garbling.
3) Step through the program with a watch on that one variable, and see at exactly which instruction it changes, then get back to us.

Just things to try... HTH

Also: Why does your constructor have a different name than you class name?

Share this post


Link to post
Share on other sites
Quote:
Which compiler are you using?

GCC 3.3

Quote:
Does 'useless_int' get garbled?

Yes. Looks like the first variable i declare in the class gets garbled.

Quote:
Set the variable to zero in the constructor (if you're not doing that already).


In fact, the constructor takes an ID variable as argument and immediately sets tex_index to the value that was passed so tex_index is inited for sure

[/quote]Try "using" the value --> 'int dummy = tex[0].tex_index' or something, later on in the program, see if that helps keep the value from garbling.
[/quote]

Well, for now it's private and i use a get method, but before using a 'get' method it was public and i often accessed it directly like 'tex[n].tex_index' (i never used tex[0] directly but i don't believe it should matter??) and it didn't help.

Quote:
Step through the program with a watch on that one variable, and see at exactly which instruction it changes, then get back to us.

I've done a little search i found it doesn't always happen. Basically, it happens when users adds a new texture object to the texture vector. Then, if you try to access the vector, like: textures[0].getID() then it always work. BUT the main goal of my program is that each 3D model can have its own texture, so every model objects hold a pointer to the appropriate texture too. if you try to getID() via this pointer it gets garbled. i thought it could be a pointer mistake, but i don't know why 'useless int' would fix pointer mistakes and besides that accessing other variables of texture #0 via the same pointer returns correct values that are typical of texture #0 so i know my pointer is ok

Quote:
Why does your constructor have a different name than you class name?

Oops... it's because i typed the 'class' line but copy-pasted others, and i forgot to type the prefix i use in front of all my class names. in my code they have the same name.

Share this post


Link to post
Share on other sites
Quote:
Original post by Marianne
I've done a little search i found it doesn't always happen. Basically, it happens when users adds a new texture object to the texture vector. Then, if you try to access the vector, like: textures[0].getID() then it always work. BUT the main goal of my program is that each 3D model can have its own texture, so every model objects hold a pointer to the appropriate texture too. if you try to getID() via this pointer it gets garbled.


Upon inserting an element, the textures container was forced to grow. Most likely, it didn't allocate enough room for more than 1 at the start, and was forced to reallocate - causing all the data to be relocated - invalidating your old pointer, as well as all iterators to the container.

Share this post


Link to post
Share on other sites
Yeh, I think the moral is: Don't store a pointer to a <vector> element, since you don't know if the address of an element will change (you don't know <vector>'s implementation). Only do that for an un-resizing traditional array.

Share this post


Link to post
Share on other sites

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