Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak #10441

Merged
merged 2 commits into from
Aug 9, 2023
Merged

fix memory leak #10441

merged 2 commits into from
Aug 9, 2023

Conversation

moehuster
Copy link
Contributor

No description provided.

@paddle-bot
Copy link

paddle-bot bot commented Jul 20, 2023

Thanks for your contribution!

@CLAassistant
Copy link

CLAassistant commented Jul 20, 2023

CLA assistant check
All committers have signed the CLA.

@shiyutang
Copy link
Collaborator

Thank you for your contribution, can you provide the procedure to reproduce the memory leak for us to validate the effectiveness of this PR.

@moehuster
Copy link
Contributor Author

Before I provide the procedure to reproduce the memory leak. Let's go to the code itself.

  StructureTableRecognizer *table_model_ = nullptr;
  StructureLayoutRecognizer *layout_model_ = nullptr;
  if (FLAGS_layout) {
    this->layout_model_ = new StructureLayoutRecognizer(
        FLAGS_layout_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
        FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_layout_dict_path,
        FLAGS_use_tensorrt, FLAGS_precision, FLAGS_layout_score_threshold,
        FLAGS_layout_nms_threshold);
  }
  if (FLAGS_table) {
    this->table_model_ = new StructureTableRecognizer(
        FLAGS_table_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
        FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_table_char_dict_path,
        FLAGS_use_tensorrt, FLAGS_precision, FLAGS_table_batch_num,
        FLAGS_table_max_len, FLAGS_merge_no_span_structure);
  }
PaddleStructure::~PaddleStructure() {
  if (this->table_model_ != nullptr) {
    delete this->table_model_;
  }
}

Just like the variable table_model_, layout_model_ is a private member of PaddleStructure. They are all initialized in the constructor. But only table_model_ was deleted in the destructor. There will be a memory leak, If the FLAGS_layout was true.
IMO, smart pointer std::unique_ptr is a better choice.

@tink2123
Copy link
Collaborator

tink2123 commented Aug 9, 2023

Thanks for your contribution, can you submit this PR to the dygraph branch?

@shiyutang shiyutang merged commit b938161 into PaddlePaddle:release/2.6 Aug 9, 2023
moehuster added a commit to moehuster/PaddleOCR that referenced this pull request Aug 10, 2023
* fix memory leak

* update: Using smart pointers instead of raw pointers
@moehuster
Copy link
Contributor Author

Thanks for your contribution, can you submit this PR to the dygraph branch?

A new PR is added #10593, TBR.

shiyutang pushed a commit that referenced this pull request Aug 14, 2023
* fix memory leak (#10441)

* fix memory leak

* update: Using smart pointers instead of raw pointers

* update: Usinig intuitive initialization of duration (#10512)
hiroi-sora added a commit to hiroi-sora/PaddleOCR-json that referenced this pull request Jul 28, 2024
1. 跟进 [PaddleOCR #10441](PaddlePaddle/PaddleOCR#10441)
- 使用智能指针代替原始指针
- 修复 detector_ classifier_ recognizer_ 可能造成的内存泄漏

2. 跟进 [PaddleOCR #10512](PaddlePaddle/PaddleOCR#10512)
- 修复时间记录的初始化过程

3. 格式化代码,补充部分注释
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants