Skip to main content
GameDev.net gamedev.net
🔒 Locked

Some problem with STL

Started by Nameless1 Nov 5, 2004 at 4:15 PM 9 replies 750+ views
Original Post
Nameless1
Nameless1
Hello, everyone^^ I am implementing my graphic program and it seems to have a serious bug with C++ STL vector. I declared a vector ActiveVertexList in a class and I am sure that it has been properly initialized. But when the program runs to a function's "return" expression, the data in ActiveVertexList are modified.......and it seems logically impossible!! The codes are like this: #include //some other include files static MyClass Mesh; //the ActiveVertexList is //MyClass's data member ... .... ..... HRESULT CALLBACK Function() { //Do something Mesh.init(); //Data are all right in this section //hr is HRESULT type return hr; //<= after this expression ,The data in //Mesh.ActiveVertexList are impossibly //Modified.......I can't figure out WHY..... } I have traced the code by VS.net 2003,and I am sure that the data are right before return expression. May this a bug of STL vector?? May someone give me a hand? I'll appreciate it very much^^ THX.
mishikel
mishikel
Yes, I can help. It's not a bug in std::vector.




Can you post more code?
chollida1
chollida1
You'd have to show us

a) how you add the data to the vector
b) how you read teh data from the vector

Off hand if its being added correctly, which I think you mention. Try chaning your iterator to a const_iterator. That way it can't be changed.

And make sure your not using the operator[], I beleive it returns a reference to the data so you could be stomping it with that.


Cheers
Chris
CheersChris
ajas95
ajas95
Dude, there's no WAY it's a problem in STL vector!!!


It's probably a compiler bug.

[edit] okay, I feel really bad posting a joke with no substance at all. So, do you have any objects that are local variables in the function that might do something in their destructors? Maybe the Mesh you call init() on?[/edit]
moucard
moucard
Also, while debuging, try to see the starting addresses of your variables. It has happened to me sometimes that a variable was partially on top of another. e.g the last two bytes of an int would fall onto the first two bytes of another int. So that could change the variables unexpectedly.
Nameless1
Nameless1
The code is Here:
http://www.cis.nctu.edu.tw/~is90055/PMesh.rar

This is a VS.net2003 solution,and it based on DirectX 9.0c.
Please remember to set Dx9 library include path before you
compile this.

This program implement View Dependent Progressive Mesh(VDPM),
and is not complete yet.
The winmain() code is in ProgressiveMesh.cpp.
And VDPM.h & VDPM.cpp implement the VDPM class.
The AVList is a vector global vector declared
in VDPM.cpp.

The prolem is in OnCreateDevice() in ProgressiveMesh.cpp.
At line 408, I initialize the VDPM,and the data I want is
all right at this point.You can use Debug tracer to see the variable's value. Data with problem is MyVDPM.VTree & AVList.
At this point, VTree._Myfirst.Parent has a value that is correct,and so is AVList._Myfirst.Parent(These two pointer's content should be the same).

But when it come to Line 503, and after the "retrun hr" executed, MyVDPM.VTree._Myfirst.Parent point to an unknown address and it's content is wrong,so is AVList._Myfirst.Parent
........because of this strange thing,the VDPM class can't work
properly.




I think Mr.chollida1 may got the point, I used many [] operator
to construct the VTree&AVList,and unlike standard C++ array, STL vector [] operator returns only a reference.

I am just a STL newbie.
Could someone try to figure out why this happened please?
Thanks very much to everyone(^__^).
MaulingMonkey
MaulingMonkey
Quote:
Original post by chollida1
And make sure your not using the operator[], I beleive it returns a reference to the data so you could be stomping it with that.


Please explain what the hell you mean? It returns a reference so you can do this:

std::vector<std::string> pie(13);pie[2] = "fnord";cout << pie[2] << endl;


If it just returned a value (not a reference), it would compile - but the 3rd element (remember, 0 based arrays) - would remain unchanged. This makes it behave just like a c-style array:

std::string pie[13];pie[2] = "fnord";cout << pie[2] << endl;


Quote:
Original post by Nameless1
The code is Here:
http://www.cis.nctu.edu.tw/~is90055/PMesh.rar


RARs are evil, use .zip or .tar.gz and I'll look at it.. (or just the .cpp file, if there's only one)
Nameless1
Nameless1
http://www.cis.nctu.edu.tw/~is90055/PMesh.zip

This is .zip version of the code.
ajas95
ajas95
Okay, here's what I really bet is going on (I'm still downloading DXSDK 9.0c, so I can't compile yet to make sure)... You construct a VDPM named Mesh then assign it to MyVDPM. Then Mesh gets destructed when the function leaves. See, when you don't define an assignment constructor for a class, the compiler generates one for you so you can do these kinds of assignments... except that it only does a shallow copy, so anything that MyVDPM points to that it inherits from Mesh via the assignment, that then gets destroyed by Mesh's destructor then becomes invalid (which would be consistent with your claim that it happens when the function returns).

SO. The way to test if this is what's going on is really easy. You make a MyVDPM a pointer to a VDPM. Then instead of:
	VDPM Mesh(pMesh,pAdjacencyBuffer);	MyVDPM=Mesh;

you just use:
MyVDPM = new VDPM(pMesh, pAdjacencyBuffer);

and then go turn the MyVDPM.foos into MyVDPM->foos , which there aren't very many so it should only take a minute (in fact it looks like that's the way it used to be).
Nameless1
Nameless1
To ajas95:

Yes!! You are right!!

Because of Mesh's Destructor,
Mesh.VTree.Parent has been released.
although MyVDPM have't been destructed at the return expression,
but MyVDPM.VTree.Parent(which is a pointer) point to a place
same as Mesh.VTree.Parent, so when Mesh.VTree.Parent been
realeased,MyVDPM.VTree.Parent is also releasing too.

That's why the problem happened.
I feel sorry for blaming STL........

Thank you very much^^.
Thx to others too^^.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.