Skip to content

Commit e74fef3

Browse files
author
Fabrice Di Meglio
committed
Fix potential leak in TextLayouCache
- need a copy constructor for the key as the GenerationCache we are using is actually a KeyedVector<K, sp<Entry<K, V> > > - use the getText() API to access the text in the cache key Change-Id: I5b60ebc062b62308ed7ac1284cfe2a9f28e2b8b1
1 parent 04e3bb6 commit e74fef3

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

core/jni/android/graphics/TextLayoutCache.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,22 @@ TextLayoutCacheKey::TextLayoutCacheKey(const SkPaint* paint,
242242
hinting = paint->getHinting();
243243
}
244244

245+
TextLayoutCacheKey::TextLayoutCacheKey(const TextLayoutCacheKey& other) :
246+
text(NULL),
247+
textCopy(other.textCopy),
248+
contextCount(other.contextCount),
249+
dirFlags(other.dirFlags),
250+
typeface(other.typeface),
251+
textSize(other.textSize),
252+
textSkewX(other.textSkewX),
253+
textScaleX(other.textScaleX),
254+
flags(other.flags),
255+
hinting(other.hinting) {
256+
if (other.text) {
257+
textCopy.setTo(other.text);
258+
}
259+
}
260+
245261
bool TextLayoutCacheKey::operator<(const TextLayoutCacheKey& rhs) const {
246262
LTE_INT(count) {
247263
LTE_INT(contextCount) {
@@ -253,7 +269,7 @@ bool TextLayoutCacheKey::operator<(const TextLayoutCacheKey& rhs) const {
253269
LTE_INT(flags) {
254270
LTE_INT(hinting) {
255271
LTE_INT(dirFlags) {
256-
return strncmp16(text, rhs.text, contextCount) < 0;
272+
return strncmp16(getText(), rhs.getText(), contextCount) < 0;
257273
}
258274
}
259275
}
@@ -269,7 +285,7 @@ bool TextLayoutCacheKey::operator<(const TextLayoutCacheKey& rhs) const {
269285

270286
void TextLayoutCacheKey::internalTextCopy() {
271287
textCopy.setTo(text, contextCount);
272-
text = textCopy.string();
288+
text = NULL;
273289
}
274290

275291
size_t TextLayoutCacheKey::getSize() {

core/jni/android/graphics/TextLayoutCache.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class TextLayoutCacheKey {
7272
const UChar* text, size_t start, size_t count,
7373
size_t contextCount, int dirFlags);
7474

75+
TextLayoutCacheKey(const TextLayoutCacheKey& other);
76+
7577
bool operator<(const TextLayoutCacheKey& rhs) const;
7678

7779
/**
@@ -86,7 +88,7 @@ class TextLayoutCacheKey {
8688
size_t getSize();
8789

8890
private:
89-
const UChar* text;
91+
const UChar* text; // if text is NULL, use textCopy
9092
String16 textCopy;
9193
size_t start;
9294
size_t count;
@@ -98,6 +100,10 @@ class TextLayoutCacheKey {
98100
SkScalar textScaleX;
99101
uint32_t flags;
100102
SkPaint::Hinting hinting;
103+
104+
inline const UChar* getText() const {
105+
return text ? text : textCopy.string();
106+
}
101107
}; // TextLayoutCacheKey
102108

103109
/*

0 commit comments

Comments
 (0)