---
name: "sd_device_unrefp"
section: "3"
platform: "linux"
package: "libsystemd-dev"
version: "257.13-1~deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/sd_device_ref"
---

# sd\_device\_unrefp(3)

Create or destroy references to a device object

Part of [sd\_device\_ref(3)](/sd_device_ref/3.md).

## NAME

sd\_device\_ref, sd\_device\_unref, sd\_device\_unrefp - Create or destroy references to a device object

## SYNOPSIS

```
#include <systemd/sd-device.h>
```

**sd\_device\* sd\_device\_ref(sd\_device \****device***);**

**sd\_device\* sd\_device\_unref(sd\_device \****device***);**

**void sd\_device\_unrefp(sd\_device \*\****device***);**

**sd\_device\_ref()** increases the internal reference counter of *device* by one.

**sd\_device\_unref()** decreases the internal reference counter of *device* by one. Once the reference count has dropped to zero, *device* is destroyed and cannot be used anymore, so further calls to **sd\_device\_ref()** or **sd\_device\_unref()** are illegal.

**sd\_device\_unrefp()** is similar to **sd\_device\_unref()** but takes a pointer to a pointer to an **sd\_device** object. This call is useful in conjunction with GCC's and LLVM's **Clean-up Variable Attribute**\[1\]. Note that this function is defined as an inline function. Use a declaration like the following, in order to allocate a device object that is freed automatically as the code block is left:

```
{
  __attribute__((cleanup(sd_device_unrefp))) sd_device *device = NULL;
  int r;
  ...
  r = sd_device_new_from_syspath(&device, "...");
  if (r < 0) {
    errno = -r;
    fprintf(stderr, "Failed to allocate device: %m\n");
  }
  ...
}
```

**sd\_device\_ref()** and **sd\_device\_unref()** execute no operation if the argument is **NULL**. **sd\_device\_unrefp()** will first dereference its argument, which must not be **NULL**, and will execute no operation if *that* is **NULL**.

## RETURN VALUE

**sd\_device\_ref()** always returns the argument, and **sd\_device\_unref()** always returns **NULL**.

## HISTORY

**sd\_device\_ref()**, **sd\_device\_unref()**, and **sd\_device\_unrefp()** were added in version 251.

## NOTES

- 1\.

  Clean-up Variable Attribute

  https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
