Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
* Changed to use unique_ptr.
* decrement reference count for json_object.

Before:

  ==11333== LEAK SUMMARY:
  ==11333==    definitely lost: 47,787 bytes in 120 blocks
  ==11333==    indirectly lost: 11,202,985 bytes in 105,883 blocks
  ==11333==      possibly lost: 46,244 bytes in 682 blocks
  ==11333==    still reachable: 20,012,541 bytes in 82,152 blocks
  ==11333==                       of which reachable via heuristic:
  ==11333==                         length64           : 152,752 bytes in 377 blocks
  ==11333==                         newarray           : 1,568 bytes in 18 blocks
  ==11333==                         multipleinheritance: 3,816 bytes in 46 blocks
  ==11333==         suppressed: 0 bytes in 0 blocks

After:

  ==26793== LEAK SUMMARY:
  ==26793==    definitely lost: 47,835 bytes in 118 blocks
  ==26793==    indirectly lost: 10,616,967 bytes in 99,871 blocks
  ==26793==      possibly lost: 48,818 bytes in 707 blocks
  ==26793==    still reachable: 20,012,629 bytes in 82,155 blocks
  ==26793==                       of which reachable via heuristic:
  ==26793==                         length64           : 152,752 bytes in 377 blocks
  ==26793==                         newarray           : 1,568 bytes in 18 blocks
  ==26793==                         multipleinheritance: 14,192 bytes in 159 blocks
  ==26793==         suppressed: 0 bytes in 0 blocks

Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
  • Loading branch information
kenhys committed Jun 17, 2024
1 parent ea6556e commit daf1616
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/virtualkeyboardanthy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ AnthyKeyboard::AnthyKeyboard()
FCITX_INSTALL_PKGDATADIR "/virtualkeyboardui/virtualkeyboardui-ja.json";
FCITX_KEYBOARD_LAYOUT()
<< "path of Japanese keyboard layout file: " << jsonPath;
loader_ = new KeyboardLayout(jsonPath);
loader_.reset(new KeyboardLayout(jsonPath));
}
#else
AnthyKeyboard::AnthyKeyboard()
Expand Down Expand Up @@ -82,7 +82,7 @@ void AnthyKeyboard::setLayerKeys(size_t offset) {
FCITX_KEYBOARD_LAYOUT()
<< "loaded size of keys: " << loader_->keys().size();
for (size_t i = 0; i < loader_->keys().size(); i++) {
keys_.emplace_back(loader_->keys()[i]);
keys_.emplace_back(std::move(loader_->keys()[i]));
}
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/virtualkeyboardanthy.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class AnthyKeyboard : public I18nKeyboard {
private:
#if USE_CUSTOM_LAYOUT
void setLayerKeys(size_t offset);
KeyboardLayout *loader_;
std::unique_ptr<KeyboardLayout> loader_;
#else
void setTextRomajiKeys();
void setMarkKeys();
Expand Down
2 changes: 1 addition & 1 deletion src/virtualkeyboardchewing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void ChewingKeyboard::setLayerKeys(size_t offset) {
FCITX_KEYBOARD_LAYOUT()
<< "loaded size of keys: " << loader_->keys().size();
for (size_t i = 0; i < loader_->keys().size(); i++) {
keys_.emplace_back(loader_->keys()[i]);
keys_.emplace_back(std::move(loader_->keys()[i]));
}
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/virtualkeyboardchewing.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class ChewingKeyboard : public I18nKeyboard {
"/virtualkeyboardui/virtualkeyboardui-zh_TW.json";
FCITX_KEYBOARD() << "path of Traditional chinese keyboard layout file: "
<< jsonPath;
loader_ = new KeyboardLayout(jsonPath);
loader_.reset(new KeyboardLayout(jsonPath));
}
#endif

private:
#if USE_CUSTOM_LAYOUT
void setLayerKeys(size_t offset);
KeyboardLayout *loader_;
std::unique_ptr<KeyboardLayout> loader_;
#else
void setTextKeys();
void setMarkKeys();
Expand Down
3 changes: 2 additions & 1 deletion src/virtualkeyboardcustom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ void CustomKeyboard::setLayerKeys(size_t offset) {
FCITX_KEYBOARD_LAYOUT()
<< "loaded size of keys: " << loader_->keys().size();
for (size_t i = 0; i < loader_->keys().size(); i++) {
keys_.emplace_back(loader_->keys()[i]);
// move ownership to custom keyboard
keys_.emplace_back(std::move(loader_->keys()[i]));
}
FCITX_KEYBOARD_LAYOUT()
<< "CustomKeyboard::setLayerKeys(): size of keys: " << keys_.size();
Expand Down
4 changes: 2 additions & 2 deletions src/virtualkeyboardcustom.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CustomKeyboard : public I18nKeyboard {
}
FCITX_KEYBOARD_LAYOUT()
<< "resolved full path of keyboard layout file: " << fullPath;
loader_ = new KeyboardLayout(fullPath);
loader_.reset(new KeyboardLayout(fullPath));
loader_->loadMetadata(0);
label_ = loader_->label();
languageCode_ = std::string(loader_->languageCode());
Expand All @@ -62,7 +62,7 @@ class CustomKeyboard : public I18nKeyboard {
const char *label_ = nullptr;
std::string languageCode_;
void setLayerKeys(size_t offset);
KeyboardLayout *loader_;
std::unique_ptr<KeyboardLayout> loader_;
int mode_ = 0;
bool isAdditionalMarkOn_ = false;
};
Expand Down
2 changes: 1 addition & 1 deletion src/virtualkeyboardhangul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void HangulKeyboard::setLayerKeys(size_t offset) {
FCITX_KEYBOARD_LAYOUT()
<< "loaded size of keys: " << loader_->keys().size();
for (size_t i = 0; i < loader_->keys().size(); i++) {
keys_.emplace_back(loader_->keys()[i]);
keys_.emplace_back(std::move(loader_->keys()[i]));
}
}
#else
Expand Down
4 changes: 2 additions & 2 deletions src/virtualkeyboardhangul.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class HangulKeyboard : public I18nKeyboard {
const char *jsonPath = FCITX_INSTALL_PKGDATADIR
"/virtualkeyboardui/virtualkeyboardui-ko.json";
FCITX_KEYBOARD() << "path of Korean keyboard layout file: " << jsonPath;
loader_ = new KeyboardLayout(jsonPath);
loader_.reset(new KeyboardLayout(jsonPath));
}
#endif

private:
#if USE_CUSTOM_LAYOUT
void setLayerKeys(size_t offset);
KeyboardLayout *loader_;
std::unique_ptr<KeyboardLayout> loader_;
#else
void setTextKeys();
void setMarkKeys();
Expand Down
14 changes: 12 additions & 2 deletions src/virtualkeyboardlayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ class KeyboardLayout {
}
}

~KeyboardLayout() {
FCITX_KEYBOARD_LAYOUT() << "~KeyboardLayout()";
keys_.clear();
stateLabels_.clear();
modeLabels_.clear();
modeActions_.clear();
modeOffsets_.clear();
// decrement the reference count
json_object_put(json_);
}
// Keyboard metadata
const char *label() { return label_; }
const char *languageCode() { return languageCode_; }
Expand All @@ -50,15 +60,15 @@ class KeyboardLayout {
bool loadMetadata(size_t offset);
bool loadKeyLayout(size_t offset);
bool load(size_t offset);
std::vector<VirtualKey *> &keys() { return keys_; }
std::vector<std::unique_ptr<VirtualKey>> &keys() { return keys_; }
std::string modeLabel(int mode) { return modeLabels_[mode]; }
std::map<std::string, int> &modeActions() { return modeActions_; }
std::map<std::string, int> &modeOffsets() { return modeOffsets_; }
int modeOffsetsFallback(size_t modeIndex,
std::map<std::string, int> conditions);

protected:
std::vector<VirtualKey *> keys_;
std::vector<std::unique_ptr<VirtualKey>> keys_;
bool parseMetadata(size_t offset);
double parseDouble(json_object *object);
bool parseKeyboard(json_object *keyboard, size_t offset);
Expand Down
2 changes: 1 addition & 1 deletion src/virtualkeyboardpinyin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void PinyinKeyboard::setLayerKeys(size_t offset) {
FCITX_KEYBOARD_LAYOUT()
<< "loaded size of keys: " << loader_->keys().size();
for (size_t i = 0; i < loader_->keys().size(); i++) {
keys_.emplace_back(loader_->keys()[i]);
keys_.emplace_back(std::move(loader_->keys()[i]));
}
}
#else
Expand Down
4 changes: 2 additions & 2 deletions src/virtualkeyboardpinyin.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class PinyinKeyboard : public I18nKeyboard {
const char *jsonPath = FCITX_INSTALL_PKGDATADIR
"/virtualkeyboardui/virtualkeyboardui-zh_CN.json";
FCITX_KEYBOARD() << "path of Korean keyboard layout file: " << jsonPath;
loader_ = new KeyboardLayout(jsonPath);
loader_.reset(new KeyboardLayout(jsonPath));
}
#endif

private:
#if USE_CUSTOM_LAYOUT
void setLayerKeys(size_t offset);
KeyboardLayout *loader_;
std::unique_ptr<KeyboardLayout> loader_;
#else
void setTextKeys();
void setMarkKeys();
Expand Down
2 changes: 1 addition & 1 deletion src/virtualkeyboardrussian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void RussianKeyboard::setLayerKeys(size_t offset) {
FCITX_KEYBOARD_LAYOUT()
<< "loaded size of keys: " << loader_->keys().size();
for (size_t i = 0; i < loader_->keys().size(); i++) {
keys_.emplace_back(loader_->keys()[i]);
keys_.emplace_back(std::move(loader_->keys()[i]));
}
}
#else
Expand Down
4 changes: 2 additions & 2 deletions src/virtualkeyboardrussian.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class RussianKeyboard : public I18nKeyboard {
"/virtualkeyboardui/virtualkeyboardui-ru.json";
FCITX_KEYBOARD() << "path of Russian keyboard layout file: "
<< jsonPath;
loader_ = new KeyboardLayout(jsonPath);
loader_.reset(new KeyboardLayout(jsonPath));
}
#endif

private:
#if USE_CUSTOM_LAYOUT
void setLayerKeys(size_t offset);
KeyboardLayout *loader_;
std::unique_ptr<KeyboardLayout> loader_;
#else
void setCyrillicTextKeys();
void setMarkKeys();
Expand Down
2 changes: 1 addition & 1 deletion src/virtualkeyboardus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void UsKeyboard::setLayerKeys(size_t offset) {
loader_->load(offset);
FCITX_KEYBOARD() << "loaded size of keys: " << loader_->keys().size();
for (size_t i = 0; i < loader_->keys().size(); i++) {
keys_.emplace_back(loader_->keys()[i]);
keys_.emplace_back(std::move(loader_->keys()[i]));
}
}
#else
Expand Down
4 changes: 2 additions & 2 deletions src/virtualkeyboardus.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class UsKeyboard : public I18nKeyboard {
"/virtualkeyboardui/virtualkeyboardui-us.json";
FCITX_KEYBOARD() << "path of English keyboard layout file: "
<< jsonPath;
loader_ = new KeyboardLayout(jsonPath);
loader_.reset(new KeyboardLayout(jsonPath));
}
#endif

private:
#if USE_CUSTOM_LAYOUT
void setLayerKeys(size_t offset);
KeyboardLayout *loader_;
std::unique_ptr<KeyboardLayout> loader_;
#else
void setTextKeys();
void setMarkKeys();
Expand Down

0 comments on commit daf1616

Please sign in to comment.