tags:

views:

479

answers:

3

I'm trying to use a std::vector<>::const_iterator and I get an 'access violation' crash. It looks like the std::vector code is crashing when it uses its own internal First_ and Last_ pointers. Presumably this is a known bug. I'm hoping someone can point me to the correct workaround. It's probably relevant that the crashing function is called from an external library?

const Thing const*  AClass::findThing (const std::string& label) const
{
    //ThingList_.begin() blows up at run time.  Compiles fine.
    for (std::vector<Thing*>::const_iterator it = ThingList_.begin(); it != ThingList_.end(); ++it) {
     //Irrelevant.
    }
    return 0;
}

Simply calling ThingList_.size() also crashes.

This is sp6, if it matters.

+4  A: 

If you're passing C++ objects across external library boundaries, you must ensure that all libraries are using the same runtime library (in particular, the same heap allocator). In practice, this means that all libraries must be linked to the DLL version of MSVCRT.

Greg Hewgill
We had the same problem with vectors back in the day. Passing them across DLL boundaries caused headaches, even using the same CRT libraries. I believe they fixed this in later releases. We havent had any similar problems in ages, anyways.
Joe
+1  A: 

It's almost certainly a bug in your code and not std::vector. This code is used by way too many projects to have such an easy to repro bug.

What's likely happening is that the ThnigList_ variable has been corrupted in some way. Was the underlying array accessed directly and/or modified?

JaredPar
The google says there are tons of horrible bugs in this STL implementation. Remember, VC6 is ancient.I will look harder at the control flow working with ThingList_, but it's a pretty straight forward private member variable. I don't see any casts or pointers to it right now.
kingkongrevenge
@kingkongrevenge, yes there are bugs but not being able to iterate a simple vector would have been a ship stopper
JaredPar
One of the bugs I saw was that you simply cannot use a std::map across shared libraries. I'd have thought that a ship stopper, but apparently they didn't catch it. I think this is a bug and it has to do with how my project is linked.
kingkongrevenge
A: 

I agree with Jared that it is probably in your code, never the less, you should be sure your stl libs are up to date.

The dinkumware site has the patched files you need.

You should update just to be safe

EvilTeach
If the vector is never cast, what else could be corrupting it? Once I double check that AClass and the vector are never improperly cast I am out of ideas. And I'm pretty sure they're not cast.
kingkongrevenge
I am not talking about cast relation corruption. I am pointing out that your #include <vector> file may be out of date, and giving you the link to the place where the updates can be found. :)
EvilTeach
Without looking at your code, I couldn't begin to guess. C and C++ are well know for boundary related defects.
EvilTeach