cudaFreeHost() fails when cleaning up with exit(0)

Hello,

I’m running into trouble exiting my program (compiled with nvcc under linux), when my objects are cleaned up after exit(0) is called

My object constructor / destructor

HostMatrix(size_t memSize) {

    printf("Allocating %d bytes to Host\n", memSize);
    int cudaStatus = cudaMallocHost((void**)&(this->data), memSize);
    this->memSize = memSize;

    memset(this->data, 0, memSize);

    if (cudaStatus != 0) {
        throw ERROR_ALLOC_HOST;
    }
}

~HostMatrix() {
    if (this->data == nullptr) {
  	return;
    }
    printf("De-allocating %d bytes from Host\n", this->memSize);
    int cudaStatus = cudaFreeHost(this->data);
    if (cudaStatus != 0) {
    	    fprintf(stderr, "Could not de-allocate %d bytes of pinned memory to the host\n", (int) this->memSize);
	throw ERROR_DEALLOC_HOST;
    }
    this->data = nullptr;
}

These are the only place in the program calling cudaMallocHost and cudaFreeHost.

It runs fine during operation:

...
De-allocating 299978018 bytes from Host
De-allocating 149989009 bytes from Host
De-allocating 299978018 bytes from Host
De-allocating 151363809 bytes from Host
De-allocating 302727618 bytes from Host
De-allocating 149989009 bytes from Host
De-allocating 299978018 bytes from Host
De-allocating 302727618 bytes from Host
De-allocating 149989009 bytes from Host
De-allocating 299978018 bytes from Host
De-allocating 149989009 bytes from Host
...

But fails after exit(0) is called:

Program terminated with success.
Could not de-allocate 151363809 bytes of pinned memory to the host
terminate called after throwing an instance of 'int'
De-allocating 151363809 bytes from Host

So I was wondering if maybe there’s some additional cleaning up introduced by nvcc before I clean up my objects myself, which already frees up the host memory ? I could not find any documentation on exiting the main() in the programming guide.

The (very likely) alternative of course is that this particular object didn’t have any data allocated in the first place, in which case it’s obviously a bug of mine…

Thanks and have a nice day !