I have a map
map<AnsiString, Foo*>
When I try to look up values in the map I use a string I have received from a TMemoryStream
and send it to a Foo* GetFoo(AnsiString)
function that in turn creates an iterator and returns the object found. This works fine in most of the cases but in ONE function it doesn't return a value.
If I do FooID = FooID.SetLength(FooID.Length() - 1);
where FooID
is the AnsiString to use for the look up it works and returns the correct result. I was thinking that there might be some garbage in the stream but when I look at the AnsiStrings used in the place that works out of the box and the place that needs the hack they are identical!?
This is according to the IDE's debug value inspector... So there might be som trickery going on there. But what in earth could be causing this???
Update:
I found the implementation of the operator>>
used.
PACKAGE TStream & operator >>(TStream &Stream, AnsiString &s)
{
int i;
Stream.ReadBuffer(&i, sizeof(i));
if (i<0)
{
WideString ws;
i=-i;
ws.SetLength(i);
if (i>0)
Stream.ReadBuffer(ws.c_bstr(), i*sizeof(wchar_t));
s=ws;
} else if (i>0)
{
s.SetLength(i);
Stream.ReadBuffer(s.c_str(), i);
} else
s="";
return Stream;
}
Stepping through it seems to handle the received strings correctly. But I seem to recall hearing that issues with WideString
is not uncommon :P
Update 2:
I actually found an instance where the string returned is 6 chars long plus the ending \0
but the size is set to 7. So I guess my solution will be in the application sending that string since it supplies the wrong size.
They are obviously not identical, since you need to truncate an extra char
to make it work correctly. So find out what that extra char
actually is. Either the stream data has garbage in it to begin with, or the code that is reading the AnsiString from the stream has a logic bug in it that is introducing garbage.
When you use the debug inspector, make sure you are taking the AnsiString's entire length into account. By default, the inspector treats an AnsiString as a null terminated string and thus stops displaying data when it encounters the first null character it finds, so you might need to use the inspector's "Range" option to see everything that is actually present in the AnsiString's memory.