Memory cleanup on exit valgrind shows several mallocs() not beeing cleaned up

Hello,

actually I am trying to debug a crash-on-exit in some other application and found this along the way:

When I run the small example code given in CL.hpp in valgrind, valgrind tells me there is some mallocs in cl::context and cl::program not cleaned up correctly.

I know this is not critical (and does not cause the crash in my other application), but maybe someone can tell me how to prevent this from happening anyways.

Below is the output of valgrind and also the corresponding example code which is taken from the original CL.hpp file.

I tested this with a Debian Testing x86_64 machine, nvidia driver 295.20.

Both are really short so please have a quick look :) Thank you!

> valgrind --leak-check=full testclass

==31362== Memcheck, a memory error detector

==31362== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.

==31362== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info

==31362== Command: testclass

==31362== 

==31362== Warning: client syscall munmap tried to modify addresses 0x0-0x6ffffffff

==31362== Warning: client syscall munmap tried to modify addresses 0x0-0x6ffffffff

==31362== Warning: client syscall munmap tried to modify addresses 0x0-0x6ffffffff

==31362== Warning: client syscall munmap tried to modify addresses 0x0-0x6ffffffff

==31362== Warning: client syscall munmap tried to modify addresses 0x0-0x6ffffffff

==31362== Warning: client syscall munmap tried to modify addresses 0x0-0x6ffffffff

==31362== Warning: set address range perms: large range [0x800000000, 0xf00000000) (noaccess)

==31362== 

==31362== HEAP SUMMARY:

==31362==     in use at exit: 106,938 bytes in 1,046 blocks

==31362==   total heap usage: 4,371 allocs, 3,325 frees, 3,128,397 bytes allocated

==31362== 

==31362== 8 bytes in 1 blocks are definitely lost in loss record 15 of 132

==31362==    at 0x402994D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==31362==    by 0x1F2B8CF4: ??? (in /usr/lib/libcuda.so.295.20)

==31362==    by 0x1F2B8838: ??? (in /usr/lib/libcuda.so.295.20)

==31362==    by 0xFC3E075: clCreateContextFromType (in /usr/lib/libOpenCL.so.1.0.0)

==31362==    by 0x408905: cl::Context::Context(unsigned long, long*, void (*)(char const*, void const*, unsigned long, void*), void*, int*) (cl.hpp:1456)

==31362==    by 0x4080E6: main (testclass.cpp:32)

==31362== 

==31362== 8 bytes in 1 blocks are definitely lost in loss record 16 of 132

==31362==    at 0x402994D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==31362==    by 0xFC3CCB6: ??? (in /usr/lib/libOpenCL.so.1.0.0)

==31362==    by 0xFC3E4E1: ??? (in /usr/lib/libOpenCL.so.1.0.0)

==31362==    by 0xFC3DE9F: clGetPlatformIDs (in /usr/lib/libOpenCL.so.1.0.0)

==31362==    by 0x4087FD: cl::Platform::get(std::vector<cl::Platform, std::allocator<cl::Platform> >*) (cl.hpp:1394)

==31362==    by 0x408039: main (testclass.cpp:24)

==31362== 

==31362== 8 bytes in 1 blocks are definitely lost in loss record 17 of 132

==31362==    at 0x402994D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==31362==    by 0x1F2BA2EC: ??? (in /usr/lib/libcuda.so.295.20)

==31362==    by 0x408C0C: cl::Program::Program(cl::Context const&, std::vector<std::pair<char const*, unsigned long>, std::allocator<std::pair<char const*, unsigned long> > > const&, int*) (cl.hpp:2409)

==31362==    by 0x4081A8: main (testclass.cpp:37)

==31362== 

==31362== LEAK SUMMARY:

==31362==    definitely lost: 24 bytes in 3 blocks

==31362==    indirectly lost: 0 bytes in 0 blocks

==31362==      possibly lost: 0 bytes in 0 blocks

==31362==    still reachable: 106,914 bytes in 1,043 blocks

==31362==         suppressed: 0 bytes in 0 blocks

==31362== Reachable blocks (those to which a pointer was found) are not shown.

==31362== To see them, rerun with: --leak-check=full --show-reachable=yes

==31362== 

==31362== For counts of detected and suppressed errors, rerun with: -v

==31362== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 9 from 9)
#define __CL_ENABLE_EXCEPTIONS

#if defined(__APPLE__) || defined(__MACOSX)

#include <OpenCL/cl.hpp>

#else

#include <CL/cl.hpp>

#endif

#include <cstdio>

#include <cstdlib>

#include <iostream>

const char  *helloStr  = "__kernel void "

                        "hello(void) "

                        "{ "

                        "  "

                        "} ";

int main(void)

{

  cl_int err = CL_SUCCESS;

  try {

std::vector<cl::Platform> platforms;

    cl::Platform::get(&platforms);

    if (platforms.size() == 0) {

      std::cout << "Platform size 0\n";

      return -1;

    }

cl_context_properties properties[] = { CL_CONTEXT_PLATFORM,

        (cl_context_properties) (platforms[0])(), 0 };

    cl::Context context(CL_DEVICE_TYPE_GPU, properties);

std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();

cl::Program::Sources source(1, std::make_pair(helloStr, strlen(helloStr)));

    cl::Program program_ = cl::Program(context, source);

    program_.build(devices);

cl::Kernel kernel(program_, "hello", &err);

cl::Event event;

    cl::CommandQueue queue(context, devices[0], 0, &err);

    queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(4, 4),

        cl::NullRange, NULL, &event);

event.wait();

  } catch (cl::Error &err) {

    std::cerr << "ERROR: " << err.what() << "(" << err.err() << ")"

        << std::endl;

  }

return EXIT_SUCCESS;

}