# Destructors in derived classes

**URL:** <https://forums.developer.nvidia.com/t/destructors-in-derived-classes/338639>\
**Category:** CUDA Programming and Performance\
**Tags:** kernel, cuda\
**Created:** [July 10, 2025, 8:22pm UTC](https://forums.developer.nvidia.com/t/destructors-in-derived-classes/338639 "2025-07-10T20:22:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![user117802](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@user117802](https://forums.developer.nvidia.com/u/user117802)\
**Post date:** [July 10, 2025, 8:22pm UTC](https://forums.developer.nvidia.com/t/destructors-in-derived-classes/338639/1 "2025-07-10T20:22:20Z")

</div>

We have C++ code that needs to run in **both** a CPU-only environment (e.g., GCC 13.x) and in a GPU-enabled one via CUDA (12.x).

What is the proper way to declare the destructors in base and in derived classes ?

Is this base and derived class destructor declaration below supported or will it lead to compilation or run-time errors?

```auto
class Base
public:
  ...
    Base() = default;
    __host__  __device__ virtual ~Base() {} 
  ...
};

class Derived : public Base {
public:
  ...
   __host__  __device__ virtual ~Derived() override { }
  ...
};
  

```

Can you suggest the proper way to declare derived class destructors in the case where we have to use these classes from either host-only or GPU code?

Thanks!

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [July 10, 2025, 8:44pm UTC](https://forums.developer.nvidia.com/t/destructors-in-derived-classes/338639/2 "2025-07-10T20:44:48Z")

</div>

You cannot use the code you have shown directly with GCC 13.x as the (only) compiler. Keywords like ` __host__ ` will trigger syntax errors.

CUDA doesn’t provide a native method to handle this; the solutions I have seen vary. One typical one I have seen is to provide a preamble to your code like:

```
#ifndef __CUDACC__
#define __host__ 
#define __device__
#endif

```

I’m not aware of anything that would be needed (differently) specific to host or derived classes, or constructors or destructors, specifically. Perhaps someone else will correct me.

[godbolt c++ example](https://godbolt.org/z/xvWqcasaK)  
[godbolt cuda example](https://godbolt.org/z/1sf67M9G8)

Note that in CUDA, it is illegal to pass class instances containing virtual functions between host and device code. Furthermore, attention would need to be given to proper usage of file name extensions, e.g. `.cpp` vs. `.cu`. A typical solution here might be to name everything with `.cpp` and then pass `-x cu` as needed when compiling with `nvcc`.

The above also assumes your function definitions will be “ordinary” C++ code.

If you need to use anything CUDA-specific, you will also need to segregate your handling between CPU-only (cannot use CUDA-specific intrinsics or syntax) and GPU-enabled (can use CUDA-specific intrinsics/syntax). The canonical method is to use [another](https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#virtual-architecture-macros) `nvcc` compiler macro. You can find various examples of such, like [here](https://stackoverflow.com/questions/16072740/the-behavior-of-cuda-arch-macro).

```
__host__  __device__ void foo(){
  #ifdef __CUDA_ARCH__
    // GPU-enabled code variant - GPU device code path here
  #else
    // CPU-only code variant
  #endif
  }

```

For the more general question of how do I make my code usable in either a GCC environment or a `nvcc` environment, there may be other issues to address. For example host code that calls a CUDA kernel simply will not work in a GCC-only, CPU-only environment. An alternate path would have to be provided for that sort of situation, as well.

---

<div class="post-metadata">

**Author:** ![Curefab](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Curefab](https://forums.developer.nvidia.com/u/Curefab)\
**Post date:** [July 10, 2025, 9:10pm UTC](https://forums.developer.nvidia.com/t/destructors-in-derived-classes/338639/3 "2025-07-10T21:10:02Z")

</div>

As an aside: Polymorphic classes /objects with `virtual` members typically store a vtable pointer per object. Accessing it could be an unwanted slow-down in Cuda code. But perhaps your application needs it.

---

<div class="post-metadata">

**Author:** ![user117802](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@user117802](https://forums.developer.nvidia.com/u/user117802)\
**Post date:** [July 10, 2025, 9:45pm UTC](https://forums.developer.nvidia.com/t/destructors-in-derived-classes/338639/4 "2025-07-10T21:45:10Z")

</div>

Thanks Robert,  
The ` __host__  __device__ ` are conditionally inserted when we are compiling with CUDA. For host-only compiles these are invisible.

I read that for non-device code, polymorphism is fully supported. For device code, both base and derived destructors need to be `= default` but that ` __device__ virtual ~base {} ... __device__ virtual ~Derived() override {} ` shouldn’t work (`Derived` having `Base` as base class).

I am trying to understand what are the proper ways to declare derived destructors and avoid pitfalls in CUDA.
