Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Toolkit Pro
  New Posts New Posts RSS Feed - Memory Leak on creating threads
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Memory Leak on creating threads

 Post Reply Post Reply
Author
Message
Kudo Shinya View Drop Down
Newbie
Newbie


Joined: 11 November 2024
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote Kudo Shinya Quote  Post ReplyReply Direct Link To This Post Topic: Memory Leak on creating threads
    Posted: 11 November 2024 at 2:11am
CJ: v22.0.0
VisualStudio: 2022 (17.11.5)
Windows: 11

I am creating numerous worker threads using _beginthreadex and terminating them with _endthreadex(0) and CloseHandle().

When taking a memory snapshot in Visual Studio, a memory leak of 480 bytes per thread occurs at 
MyApp.exe!_AFX_THREAD_STATE.

The call stack is as follows, confirming that the memory was allocated by the Toolkit during thread creation: 
kernel32.dll!0x7ff96021257d() 
MyApp.exe!CXTPSkinManagerDetoursApiHook::ThreadProcHook() - Line 591 
MyApp.exe!AfxGetModuleState() - Line 314 
MyApp.exe!CThreadLocal<_AFX_THREAD_STATE>CreateObject() - Line 200 
MyApp.exe!CThreadLocalObjectGetData() - Line 453 
MyApp.exe!CThreadLocal<_AFX_THREAD_STATE>::GetData() - Line 179 
MyApp.exe!CThreadLocal<_AFX_THREAD_STATE>::operator _AFX_THREAD_STATE *() - Line 191

It seems that using AfxBeginThread instead of _beginthreadex can avoid this issue

My application creates and deletes a very large number of threads, so I cannot overlook this small memory leak. 
Additionally, the libraries my application depends on also seem to create threads, making it impossible to replace all thread creation with AfxBeginThread.

Is there any workaround for this issue?

Regars,
Shinya
Back to Top
agontarenko View Drop Down
Admin Group
Admin Group


Joined: 25 March 2016
Status: Offline
Points: 299
Post Options Post Options   Thanks (0) Thanks(0)   Quote agontarenko Quote  Post ReplyReply Direct Link To This Post Posted: 14 November 2024 at 2:45am
Hello Shinya,

Unfortunately it is not clear what exactly is going wrong. Can you please provide more detailed information.
If it cannot be re-produced in any of our sample applications provided
I would appreciate you sending a sample application so that we could debug it.

Regards,
Artem Gontarenko
Back to Top
Kudo Shinya View Drop Down
Newbie
Newbie


Joined: 11 November 2024
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote Kudo Shinya Quote  Post ReplyReply Direct Link To This Post Posted: 14 November 2024 at 4:07am

Thank you for your response.
After further investigation, I was able to resolve the issue myself.

  1. The memory leak occurs not only with _beginthreadex but also with CreateThread.
  2. Calling AfxTermThread() at the end of the created thread resolves the memory leak.

By calling AfxTermThread() at the end of threads created with _beginthreadex and CreateThread, I was able to solve the problem.

Thank you.

Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.250 seconds.