Re: STL vector<float> crash

From: E. Mark Ping (emarkp_at_soda.csua.berkeley.edu)
Date: 03/02/05


Date: Wed, 2 Mar 2005 00:27:29 +0000 (UTC)

In article <ldqbpkt6scv9.dlg@ID-222894.news.individual.net>,
Active8 <reply2group@ndbbm.net> wrote:
>The vector loaded without crashing. It crashed when it went out of
>scope.

Which is when the destructor is called. If the state of the vector
gets corrupted, that's where it's likely to crash.

>How would I check going off the end during load other than using
>something like i<512 in a for loop instead of the feof() check?

One way would be what I suggested below, which is explicitly load the
number of values indicated in the header (and shrink if the actual
number read is less than that). Otherwise you're asking for a buffer
overrun (in fact, I believe precisely this kind of issue was the
source of the MS GDI+ bug).

>that's an idea. I didn't know iterators were unsafe. What's the
>point in having them?

Iterators aren't unsafe, but they aren't guaranteed to be pointer
types. Hence if you want a real pointer to what 'it' is an iterator
over, you use: &(*it). The operator* may involve more than a simple
pointer dereference (indeed, for reverse iterators that commonly the
case).

>I tried your suggestion:
>
>fread(&vec[0], sizeof(vec_type), itemCount, m_channel);
>
>and it seems to work. I can see where using the feof() return to
>stop the load isn't a good idea, especially since I'll probably add
>trailer text later. Yet I used:
>
>while( it != vec.end() )
> fwrite(it++, sizeof(vec_type), 1, m_channel);
>
>For the write function.

That also looks reasonable.

>As much as I'd like to know *why* it crashed... I bet it was that
>feof() thing causing the vector to overload and it just didn't crash
>'till it went out of scope.

Yes, probably it ran off the end of the array, and when the vector
destructor tried to deallocate, the bytes marking the end were trashed
and the allocator barfed (to use technical terms).

>Thanks, EMP.

You're welcome.

-- 
Mark Ping                     
emarkp@soda.CSUA.Berkeley.EDU 


Relevant Pages

  • Re: is such exception handling approach good?
    ... No exception from this rule that I know of (no pun ... For the object pointed by smart pointer, ... Free that resource in the destructor. ... For resources other than the heap memory, you would use other classes to ...
    (microsoft.public.vc.language)
  • Re: Returning an unknown number of types/values
    ... >> destructor is empty the Base destructor will still clear up the memory ... This array was allocated to the unsigned char pointerin the Base class's ... The useful thing about this pointer ... THe probelm appears when you try to overload the second subscript operator ...
    (alt.comp.lang.learn.c-cpp)
  • Re: reference to pointer of derived class
    ... > of a class in which I have a pointer to a pure virtual base class ... Garage(): pCar ... if the Car class has a virtual destructor. ...
    (comp.lang.cpp)
  • Re: problems with "new"
    ... I havent deleted somefoo. ... Set a breakpoint on somefoo's destructor ... make sure that there's no possibility that the pointer has been passed ... gSomefoo = fooFactory.GetInstance; ...
    (microsoft.public.vc.language)