Faulty [] operator on TBuf in 3rd Edition

Login to reply to this topic.
Tue, 2007-01-02 23:50
Joined: 2004-05-29
Forum posts: 149
Here's a fun little undocumented (maybe bug, maybe not) "feature" of Symbian OS 9 (3rd Edition) which used to work in the second edition.

Works on 2nd, not on 3rd edition:
Code:
TBuf<32> buf;
RDebug::Print(_L("len: %d buflen: %d %d"), strlen(aFileName), buf.Length(), buf.MaxLength() );  // outputs 0, 32
for (size_t i = 0; i <= strlen(aFileName); i++) {
buf[i] = aFileName[i];
}

Now it works on the 3rd edition:
Code:
TBuf<32> buf;
buf = _L("12345678901234567890123456789012");
RDebug::Print(_L("len: %d buflen: %d %d"), strlen(aFileName), buf.Length(), buf.MaxLength() ); // outputs 32, 32
for (size_t i = 0; i <= strlen(aFileName); i++) {
buf[i] = aFileName[i];
}

The deal was that the operator[] call on buf was causing a USER 9 panic.  This never happened in the 2nd Edition.  I think the problem would be solved if you added buf.SetMax(); after the declaration, although I hadn't tested that out.

Since I spent way too long tracking down this problem, I just thought you guys might want to know.

-euroq

Wed, 2007-01-03 05:43
Forum Nokia Champion
Joined: 2004-05-26
Forum posts: 732
Re: Faulty [] operator on TBuf in 3rd Edition
Quote
USER 9, if anIndex is negative or is greater than or equal to the current length of this descriptor.

Now check your for loop: it's i = 0; i <= strlen(aFileName) so that means in this case 0 - 32, right.

Now see this SDK Help:

Quote
TInt anIndex The position the data item within this descriptor's data. This is an offset value; a zero value refers to the leftmost data position.

So I think anIndex is becoming greater than the current length of this descriptor.

try changing the for loop to: i = 0; i < strlen(aFileName); may work.

Wed, 2007-01-03 16:38
Joined: 2004-05-29
Forum posts: 149
Re: Faulty [] operator on TBuf in 3rd Edition
Actually, aFileName was usually of length 10 or so.  The problem is that you simply can't do this:
Code:
TBuf<32> buf;
buf[0] = 'A';       // will cause USER 9 because buf's length is 0
On 2nd edition, that code would work; on 3rd edition, I'm presuming that the operator[] has changed its connotation to be used as an accessor instead of a modifier.  For anotherwords, I presume this will work on the 3rd edition:
Code:
TBuf<32> buf;
buf = _L("ABC");
TChar c = buf[0];    // length of buf is 3, so buf[0] works
buf[0] = 'A';           // now this works because length is 3
Wed, 2007-01-03 21:13
Joined: 2004-07-17
Forum posts: 110
Re: Faulty [] operator on TBuf in 3rd Edition
TBuf<32> buf;
buf[0] = 'A';       // will cause USER 9 because buf's length is 0

I'd be very surprised if this code behaves differently on S60 second edition. The behaviour here was determined a very long time ago (EPOC ER5 and earlier I'm sure)

A newly created TBuf is empty, so it doesn't have any elements to access. It has always been this way. Are you absolutely sure that you are not getting a panic on S60 V2?
Thu, 2007-01-04 21:19
Joined: 2004-05-29
Forum posts: 149
Re: Faulty [] operator on TBuf in 3rd Edition
Whooooops!   Embarassed

I stand corrected.  It never worked on 2nd edition.  This whole time I thought that a TBuf was an array.  My bad.

It turns out that while stress testing the application something happened which never happened in the 2nd edition.

-euroq
  • Login to reply to this topic.