内存泄漏问题
Memory leak problem
我有一个简单的程序,它创建一个线程,并在该线程结束时等待,然后程序也结束。当我用C(gcc)编译器编译这个程序,并用valgrind检查它时,没有出现问题,但当我用C++(g++)编译器编译它,并用valgrind检查时,表明我的程序存在内存泄漏。可能是什么问题?
这是我的程序
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
unsigned char b = 0;
void* threadfunc1( void *pVoid )
{
while( b == 0 )
{
usleep(10000);
}
pthread_exit(0);
}
int main(void)
{
int status;
pthread_attr_t tattr;
pthread_t thread1;
status = pthread_attr_init(&tattr);
status = pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_JOINABLE);
status = pthread_attr_setscope(&tattr,PTHREAD_SCOPE_SYSTEM);
if( pthread_create( &thread1, &tattr, threadfunc1, NULL ) != 0 )
{
exit(1);
}
usleep(1000000);
b = 1;
pthread_join( thread1, NULL);
usleep(2000000);
return 0;
}
这就是结果,当我使用g++编译它并检查valgrind 时
==7658== HEAP SUMMARY:
==7658== in use at exit: 28 bytes in 1 blocks
==7658== total heap usage: 2 allocs, 1 frees, 172 bytes allocated
==7658==
==7658== 28 bytes in 1 blocks are still reachable in loss record 1 of 1
==7658== at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==7658== by 0x400C01E: _dl_map_object_deps (dl-deps.c:506)
==7658== by 0x40117E0: dl_open_worker (dl-open.c:297)
==7658== by 0x400D485: _dl_catch_error (dl-error.c:178)
==7658== by 0x401119F: _dl_open (dl-open.c:586)
==7658== by 0x428D0C1: do_dlopen (dl-libc.c:86)
==7658== by 0x400D485: _dl_catch_error (dl-error.c:178)
==7658== by 0x428D1C0: dlerror_run (dl-libc.c:47)
==7658== by 0x428D2DA: __libc_dlopen_mode (dl-libc.c:160)
==7658== by 0x4048876: pthread_cancel_init (unwind-forcedunwind.c:53)
==7658== by 0x40489EC: _Unwind_ForcedUnwind (unwind-forcedunwind.c:126)
==7658== by 0x40464B7: __pthread_unwind (unwind.c:130)
==7658==
==7658== LEAK SUMMARY:
==7658== definitely lost: 0 bytes in 0 blocks
==7658== indirectly lost: 0 bytes in 0 blocks
==7658== possibly lost: 0 bytes in 0 blocks
==7658== still reachable: 28 bytes in 1 blocks
==7658== suppressed: 0 bytes in 0 blocks
所以,我做错了什么,是我的错误还是。。。,为什么当我用gcc编译它时没有出现问题,而当我用C++编译它时却存在内存泄漏?
@Kiril Kirov的回答已经指出,您的程序中没有内存泄漏。
但我没有看到对的呼吁
int pthread_attr_destroy(pthread_attr_t *attr);
您的程序没有内存泄漏,您有
==7658== definitely lost: 0 bytes in 0 blocks
==7658== indirectly lost: 0 bytes in 0 blocks
==7658== possibly lost: 0 bytes in 0 blocks
"仍然可访问"不意味着内存泄漏。
这里有很多关于valgrind"仍然可以到达"的问题。其中一些:
- Valgrind检测到仍然可达到的泄漏
- 仍然可以到达瓦尔grind
- Valgrind声称有未解冻的记忆。这不好吗
相关文章:
- 这个极客对极客的trie实现是否存在内存泄漏问题
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 内存泄漏问题
- 我在内存泄漏和字符串方面遇到了一个非常奇怪的问题
- 存在内存泄漏问题的链接列表
- 使用 CRT 库 - _CrtDumpMemoryLeaks() 问题的托管C++C++内存泄漏
- 如何解决C++代码中的内存泄漏问题
- 检查内存泄漏问题
- 解决将对象传递给抛出的函数时可能出现的内存泄漏问题
- 此代码是否会导致内存泄漏问题
- 'Resource Acquisition Is Initialization'解决内存泄漏问题
- C++:这个代码有什么问题吗?它编译得很好,但人们告诉我会有内存泄漏
- 在简单的哈希表中解决内存泄漏和语法问题
- 内存泄漏/瓦尔格林德问题
- 程序的内存使用量随着时间的推移而增加,但内存泄漏工具报告没有问题
- 如果对象将在消息处理程序中使用,如何解决内存泄漏问题
- C++内存泄漏问题
- C++中管理内存泄漏的问题
- 问题与OpenCV 3.0 -录制视频内存泄漏
- 内存泄漏问题c++程序