问题描述
大家好,
我正在尝试编写我的第一个多线程程序.
我有一个看起来像这样的线程:
Hello everyone,
I''m trying to write my first multithreaded program.
I have a thread that looks like this:
UINT TestThread(LPVOID lpData)
{
while(true){
// stuff to do (will be serial communication)
Sleep(500);
}
return 0;
}
我从main像这样调用:
Which I call from main like this:
AfxBeginThread(TestThread,NULL,THREAD_PRIORITY_NORMAL);
问题:调试期间出现内存泄漏:
检测到内存泄漏!
转储对象->
f:\ dd \ vctools \ vc7libs \ ship \ atlmfc \ src \ mfc \ thrdcore.cpp(306):{108}客户端块位于0x0039C728,子类型c0,长68个字节.
$ 0039C728处的一个CWinThread对象,长68个字节
第一次我要处理内存泄漏,因此我用Google搜索并找到了一种方法来定位"泄漏(嗯...用大约5行代码定位就不多了)
遵循 http://www.codeguru.com/forum/showthread.php?t=499952 [ ^ ],使用断点和 {,, msvcr90d.dll} _crtBreakAlloc ,它指向我作为问题根源的AfxBeginThread行.
如果我不在其中放置一会儿循环,那就很好用了.
我觉得也许我误解了线程应该如何工作的概念:)
因为如果从概念上讲不允许我在其中放置"while",则线程概念有点松散了它的用途……至少对于我想做的事情.
非常感谢您给出的任何答案,要么解释了如何解决内存泄漏问题,要么为什么我不允许将while(true)放在线程中(或者可能都放在::))
好的,似乎还有一个更根本的问题.如果使用,我也会发生内存泄漏.
Problem: I get a memory leak during debugging:
Detected memory leaks!
Dumping objects ->
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\thrdcore.cpp(306) : {108} client block at 0x0039C728, subtype c0, 68 bytes long.
a CWinThread object at $0039C728, 68 bytes long
First time I''m dealing with memory leaks so I googled and found a way to try to "locate" the leak (well ... not much to locate with about 5 lines of code)
following the instructions on http://www.codeguru.com/forum/showthread.php?t=499952[^], using a breakpoint and {,,msvcr90d.dll}_crtBreakAlloc and it points me to the AfxBeginThread Line as source of the problem.
If I don''t put a while loop in there, it works fine.
I feel like maybe I misunderstood the concept of how threading is supposed to work :)
Because if I''m conceptually not allowed to put the "while" there, the threading concept kind of looses it''s use ... for what I''m trying to do, at least.
Thanks a lot for any answers either explaining how to fix the memory leak problem OR why I''m not allowed to put while(true) in a thread (or maybe both :))
Ok, there seems to be a more fundamental problem. I ALSO get a memory leak if I use.
std::cout << "Starting Thread ..." << std::endl;
现在我真的迷路了,非常感谢您的帮助.
Now I''m really lost, really appreciate any help.
推荐答案
std::cout << "Starting Thread ..." << std::endl;
现在我真的迷失了,非常感谢任何帮助.
Now I''m really lost, really appreciate any help.
这篇关于while循环内部线程会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!