Crashing with MyVector.clear()
In my extension, I have tried to reset/empty a vector with MyVector.resize(0) and MyVector.clear(), but it always crashes the runtime! Wherever I use the resize() function it works fine, but if I do clear() or resize(0) it crashes!! Why is this?
EDIT: Just FYI I already did google searching and nothing I found had any information on the problem or how to solve, all the results I found either hadn't been solved yet, or the person with the problem said they fixed it and didn't say how.
Re: Crashing with MyVector.clear()
Not nearly enough information.
Presumably this is an STL vector? Give us some context - where are you using the vector, how are you using it, what are you storing in it (e.g. data or pointers?). If you're storing objects directly in the vector instead of pointers, that's a recipe for all sorts of unintended and subtle side-effects that can result in crashing your application.
Likewise if you're holding an iterator to the vector, operations like resize will invalidate them.
Re: Crashing with MyVector.clear()
Try debugging your extension to find out where the crash is. If you are using VC++ it will break on the line where the error occured.
Otherwise, can you loop through the vector and delete each one manually with MyVector.erase()?
Re: Crashing with MyVector.clear()
The line it crashes on is the line with MyVector.clear()
As for more info, sorry about that I was frustrated.
The vectors are defined in data.h and i use the rdPtr->MyVector to access them.
The only thing I do to them is resize them with resize(new size, new data) and set values in them with MyVector[position] = data; (and I make sure this never goes out of range)
The resizing happens in a function, and the value modifying happens directly in actions and in another function.
Re: Crashing with MyVector.clear()
If you are using STL, You shouldn't need to resize it, it will do it automatically.
All you do is MyVector.push_back( data ) which will add a new entry to the back of the list.
Re: Crashing with MyVector.clear()
When you use objects in your rundata structure you need to store them as pointers and create them in the CreateObject function in runtime.cpp, rather than storing them directly in the struct. The reason for this is that MMF doesn't know the content of the rundata structure, only the size, and will only allocate the memory needed. No constructors will be called and any object stored in the struct will result in garbage data.
The reason your example is crashing is most likely because the vector hasn't been properly created and therefore it's internal data is messed up, causing it to crash whenever you try to use it.
Try storing the object as a pointer in your rundata structure, like:
Code:
Vector<int>* MyVector;
Then, in CreateObject use:
Code:
rdPtr->MyVector = new Vector();
And to avoid memory leaks you should also put the following in the DestroyObject function:
Code:
if(rdPtr->MyVector != null) delete rdPtr->MyVector;
rdPtr->MyVector = null;
Note that you will have to use the -> operator instead of . when you want to use the vector as the object now is a pointer, like:
Code:
rdPtr->MyVector->clear();
You can also use the * operator to 'revert' one pointer depth, like
Code:
(*rdPtr->MyVector)[i] = 0;
Re: Crashing with MyVector.clear()
Just for reference, it's vector, not Vector.
Re: Crashing with MyVector.clear()
Werbad,I did exactly what you said and now I am getting all these errors:
Code:
1>.\Runtime.cpp(40) : error C2955: 'std::vector' : use of class template requires template argument list
1> C:\Program Files\Microsoft Visual Studio 9.0\VC\include\vector(438) : see declaration of 'std::vector'
1>.\Runtime.cpp(40) : error C2512: 'std::vector' : no appropriate default constructor available
1>.\Runtime.cpp(41) : error C2512: 'std::vector' : no appropriate default constructor available
1>.\Runtime.cpp(61) : error C2065: 'null' : undeclared identifier
1>.\Runtime.cpp(62) : error C2065: 'null' : undeclared identifier
1>.\Runtime.cpp(64) : error C2065: 'null' : undeclared identifier
1>.\Runtime.cpp(65) : error C2065: 'null' : undeclared identifier
1>Main.cpp
1>.\Main.cpp(83) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=std::string
1> ]
1> C:\Program Files\Microsoft SDKs\Windows\v6.0A\\include\guiddef.h(192): could be 'int operator ==(const GUID &,const GUID &)'
1> while trying to match the argument list '(std::vector<_Ty>, std::string)'
1> with
1> [
1> _Ty=std::string
1> ]
...which leads to more errors in places that were just fine before. I think I misunderstood you somewhere?
Re: Crashing with MyVector.clear()
Just posting corrections (I gave them to him on Gwerdy):
It's NULL, not null.
As I already said, vector, not Vector.
rdPtr->MyVector = new Vector();
has to be
rdPtr->MyVector = new vector<int>;
Re: Crashing with MyVector.clear()
Thanks Looki. Also as you told me I can't use rdPtr->MyVector[position] and it has to be (*rdPtr->Myvector)[position]
Re: Crashing with MyVector.clear()
Sorry about the confusion, I wrote that from my head and it seems I've gotten a bit too used to Java's standards...