1

There's a really strange issue in a stress test. The issue can not be 100% reproduced, so I used following codes and successfully reproduced it:

269 >---String16 test1 = String16("0");
270 >---String16 test2 = String16("0");
271 >---String16 test3 = String16("1");
272 >---String16 test4 = String16("1");
273 >---String16 test5 = String16("12");
274 >---String8 test6  = String8("12");
275 >---const char* s1 = String8(test1).string();
276 >---PLOGI("s1: %s", s1);
277 >---const char* s2 = String8(test2);
278 >---PLOGI("s2: %s", s2);
279 >---const char* s3 = String8(test3).string();
280 >---PLOGI("s3: %s", s3);
281 >---const char* s4 = String8(test4);
282 >---PLOGI("s4: %s", s4);
283 >---const char* s5 = String8(test5);
284 >---PLOGI("s5: %s", s5);
285 >---const char* s6 = test6;
286 >---PLOGI("s6: %s", s6);

Most of the logs output are as expected:

2432 I/PHService(  127): s1: 0
2433 I/PHService(  127): s2: 0
2434 I/PHService(  127): s3: 1
2435 I/PHService(  127): s4: 1
2436 I/PHService(  127): s5: 12
2437 I/PHService(  127): s6: 12

But few of them are like:

2458 I/PHService(  127): s1: ^X
2459 I/PHService(  127): s2: ^X
2460 I/PHService(  127): s3: ^X
2461 I/PHService(  127): s4: ^X
2462 I/PHService(  127): s5: ^X
2463 I/PHService(  127): s6: 12

It seems that the data after converted was corrupted(^X is the corrupted data), but it only happened to String8(Strng16) but not to String8(). I'm not sure how this happened. I used operator

String8::operator const char*() const

which should be workable for String8(Strng16) as well.

user3441073
  • 13
  • 1
  • 4
  • 1
    In the initializers of `s1`, `s2`,... `s5` you create *temporary* objects of type `String8`. Each of them is short-lived and is destroyed upon the end of the evaluation of the whole expression where it is created. Therefore when it comes to the next line where you examine the data pointed to, the pointer is likely to point to some memory that's been already freed - and probably reused. – ach Mar 20 '14 at 09:07
  • `String8()` is most probably implemented by setting the string pointer to a static empty string buffer, therefore there's no memory to be freed. – ach Mar 20 '14 at 09:09
  • see [this](http://stackoverflow.com/questions/7361239/how-to-convert-a-string-to-char) also refer [here](http://stackoverflow.com/questions/4373101/what-is-the-operator-be-overloading-here-string8operator-const-char-const) – user3355820 Mar 20 '14 at 09:09
  • @AndreyChernyakhovskiy , Thank you for you reply. Please repaste your comments as an answer so I can close the question. Thanks again. – user3441073 Mar 20 '14 at 09:20

2 Answers2

1

In the initializers of s1, s2,... s5 you create temporary objects of type String8. Each of them is short-lived and is destroyed upon the end of the evaluation of the whole expression where it is created.

Therefore when it comes to the next line where you examine the data pointed to, the pointer is likely to point to some memory that's been already freed - and probably reused.

String8() is most probably implemented by setting the string pointer to a static empty string buffer, therefore there's no memory to be freed.

ach
  • 2,314
  • 1
  • 13
  • 23
0

Either use LOGE or printf .

printf("I print a string16 '%s' ", String8(str).string());
frogatto
  • 28,539
  • 11
  • 83
  • 129
Joe
  • 841
  • 2
  • 10
  • 25