Re: STL vector<float> crash
From: E. Mark Ping (emarkp_at_soda.csua.berkeley.edu)
Date: 03/02/05
- Next message: Matt Bitten: "Re: How to declare friendship-ness to all inherited class?"
- Previous message: Active8: "Re: STL vector<float> crash"
- In reply to: Active8: "Re: STL vector<float> crash"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Matt Bitten: "Re: How to declare friendship-ness to all inherited class?"
- Previous message: Active8: "Re: STL vector<float> crash"
- In reply to: Active8: "Re: STL vector<float> crash"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|