我有一个由 cmake 构建的项目。我想用 AddressSanitizer 构建它来检测内存泄漏。我将这些行添加到 CMakeLists.txt 中:
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_STATIC_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
Run Code Online (Sandbox Code Playgroud)
但它不起作用(它没有显示我哪里有内存泄漏)。我用一个肯定包含内存泄漏的测试应用程序进行了测试,但仍然没有发生任何事情。有人可以解释我应该怎么做吗?
嗨,我目前正在修复我的 valgrind 错误,它们是:
==11925== ERROR SUMMARY: 9 errors from 1 contexts (suppressed: 0 from 0)
==11925==
==11925== 9 errors in context 1 of 1:
==11925== Syscall param ioctl(generic) points to uninitialised byte(s)
==11925== at 0xF8B7F47: ioctl (syscall-template.S:84)
==11925== by 0x1F770DAD: drmIoctl (in /opt/amdgpu/lib/x86_64-linux-gnu/libdrm.so.2.4.0)
==11925== by 0x1F7756E8: drmCommandWriteRead (in /opt/amdgpu/lib/x86_64-linux-gnu/libdrm.so.2.4.0)
==11925== by 0x3332C6AC: amdgpu_create_bo_from_user_mem (in /opt/amdgpu/lib/x86_64-linux-gnu/libdrm_amdgpu.so.1.0.0)
==11925== by 0x32A479F2: ??? (in /usr/lib/x86_64-linux-gnu/gallium-pipe/pipe_radeonsi.so)
==11925== by 0x32A6E6B3: ??? (in /usr/lib/x86_64-linux-gnu/gallium-pipe/pipe_radeonsi.so)
==11925== by 0x2D8E8BD6: ??? (in /usr/lib/x86_64-linux-gnu/libMesaOpenCL.so.1.0.0)
==11925== by 0x2D8E05D4: ??? (in /usr/lib/x86_64-linux-gnu/libMesaOpenCL.so.1.0.0)
==11925== …Run Code Online (Sandbox Code Playgroud) 我尝试编写一个包含2个线程的代码,它应该将2个数字xy递增到100,每次增量发生时,都应打印出来.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *inc_x(void *x_void_ptr){
int *x_ptr = (int *)x_void_ptr;
while (++(*x_ptr)<100) {
printf("x increment to %d \n",*x_ptr);
*x_ptr++;
}
return NULL;
}
void *inc_y(void *y_void_ptr){
int *y_ptr = (int *)y_void_ptr;
while (++(*y_ptr)<100) {
printf("y increment to %d \n",*y_ptr);
*y_ptr++;
}
return NULL;
}
int main()
{
int x = 0;
int y = 0;
printf("x: 0 , y : 0\n");
pthread_t inc_x_thread, inc_y_thread;
if (pthread_create(&inc_x_thread, NULL, inc_x, &x)) {
fprintf(stderr, "Error creating thread \n"); …Run Code Online (Sandbox Code Playgroud) 我在java中进行了关于继承的练习:
public class Gran {
private int x;
public Gran() {
this.x = 68;
}
public int age() {
this.x = this.x+1; return this.x;
}
@Override
public String toString() {
return "Gran " + age();
}
}
public class Dad extends Gran {
private int x;
public Dad(){
this.x = 41;
}
@Override
public String toString() {
return "Dad " + age();
}
}
public class Sis extends Dad {
private int x;
public Sis() {
this.x = 17;
} …Run Code Online (Sandbox Code Playgroud)