0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder / memory leak > TStringListの扱いを変更して対処 > リーク無しになった

Last updated at Posted at 2017-04-07
動作環境
C++ Builder XE4
FastMM

メモリーリーク対策中。

症状

以下のような外部ファイルを実装していた。

  • コンストラクタ
    • TStringList変数を確保
  • SetDataStr()
    • 未初期化時にはTStringList変数 = new()
    • 引数の文字列をDelimitedTextでTStringListに格納
  • GetDataStr()
    • TStringListに格納した項目をインデックス指定で取得
  • デストラクタ
    • TStringList変数を解放

上記の処理を実行していて、確認したところ、一度だけnew()をしていた。
ところが、FastMM自体では30回近くDelimitedTextでメモリリークが発生しているとなった。

対策

以下とした。

  • TStringList型でなく、String型のprivate変数を用意
  • コンストラクタ
    • 何もしない
  • SetDataStr()
    • private変数に格納
  • GetDataStr()
    • std::unique_ptr<>にてTStringList変数を宣言、初期化
    • private変数のString文字列をTStringList変数にDelimitedText指定で渡す
    • 指定のインデックスの項目をreturn
  • デストラクタ
    • private変数にL""を格納 (不要かもしれない)

上記のファイルに関するメモリリークはなくなった。

FastMMが誤って報告している可能性はある。
GetDataStr()のたびにTStringListを作るのは効率は悪いかもしれない。

結果

こちらの修正をして、かつFastMMによるメモリリークを避けるためFastMMをはずしてビルドしなおした。

修正前は30分で2MBのリークがあったが、修正後は30分で0MBのリークになった。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?