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

# sd\_event\_source\_ref(3)

Increase or decrease event source reference counters

Part of [sd\_event\_source\_unref(3)](/sd_event_source_unref/3.md).

## NAME

sd\_event\_source\_unref, sd\_event\_source\_unrefp, sd\_event\_source\_ref, sd\_event\_source\_disable\_unref, sd\_event\_source\_disable\_unrefp - Increase or decrease event source reference counters

## SYNOPSIS

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

**sd\_event\_source\* sd\_event\_source\_unref(sd\_event\_source \****source***);**

**void sd\_event\_source\_unrefp(sd\_event\_source \*\****source***);**

**sd\_event\_source\* sd\_event\_source\_ref(sd\_event\_source \****source***);**

**sd\_event\_source\* sd\_event\_source\_disable\_unref(sd\_event\_source \****source***);**

**void sd\_event\_source\_disable\_unrefp(sd\_event\_source \*\****source***);**

## DESCRIPTION

**sd\_event\_source\_unref()** may be used to decrement by one the internal reference counter of the event source object specified as *source*. The reference counter is initially set to one, when the event source is created with calls such as [**sd\_event\_add\_io**(3)](/sd_event_add_io/3.md) or [**sd\_event\_add\_time**(3)](/sd_event_add_time/3.md). When the reference counter reaches zero, the object is detached from the event loop object and destroyed.

**sd\_event\_source\_unrefp()** is similar to **sd\_event\_source\_unref()** but takes a pointer to a pointer to an **sd\_event\_source** 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 inline function.

**sd\_event\_source\_ref()** may be used to increase by one the internal reference counter of the event source object specified as *source*.

**sd\_event\_source\_unref()**, **sd\_bus\_creds\_unrefp()** and **sd\_bus\_creds\_ref()** execute no operation if the passed event source object is **NULL**.

Note that event source objects stay alive and may be dispatched as long as they have a reference counter greater than zero. In order to drop a reference of an event source and make sure the associated event source handler function is not called anymore it is recommended to combine a call of **sd\_event\_source\_unref()** with a prior call to **sd\_event\_source\_set\_enabled()** with **SD\_EVENT\_OFF** or call **sd\_event\_source\_disable\_unref()**, see below.

**sd\_event\_source\_disable\_unref()** combines a call to **sd\_event\_source\_set\_enabled()** with **SD\_EVENT\_OFF** with **sd\_event\_source\_unref()**. This ensures that the source is disabled before the local reference to it is lost. The *source* parameter is allowed to be **NULL**.

**sd\_event\_source\_disable\_unrefp()** is similar to **sd\_event\_source\_unrefp()**, but in addition disables the source first. This call is useful in conjunction with GCC's and LLVM's **Clean-up Variable Attribute**\[1\]. Note that this function is defined as inline function.

## RETURN VALUE

**sd\_event\_source\_unref()** and **sd\_event\_source\_disable\_unref()** always return **NULL**. **sd\_event\_source\_ref()** always returns the event source object passed in.

## NOTES

Functions described here are available as a shared library, which can be compiled against and linked to with the **libsystemd** [**pkg-config**(1)](/pkg-config/1.md) file.

The code described here uses [**getenv**(3)](/getenv/3.md), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call [**setenv**(3)](/setenv/3.md) from a parallel thread. It is recommended to only do calls to **setenv()** from an early phase of the program when no other threads have been started.

## HISTORY

**sd\_event\_source\_unref()**, **sd\_event\_source\_unrefp()**, and **sd\_event\_source\_ref()** were added in version 229.

**sd\_event\_source\_disable\_unref()** and **sd\_event\_source\_disable\_unrefp()** were added in version 243.

## SEE ALSO

[**sd-event**(3)](/sd-event/3.md), [**sd\_event\_add\_io**(3)](/sd_event_add_io/3.md), [**sd\_event\_add\_time**(3)](/sd_event_add_time/3.md), [**sd\_event\_add\_signal**(3)](/sd_event_add_signal/3.md), [**sd\_event\_add\_child**(3)](/sd_event_add_child/3.md), [**sd\_event\_add\_inotify**(3)](/sd_event_add_inotify/3.md), [**sd\_event\_add\_defer**(3)](/sd_event_add_defer/3.md), [**sd\_event\_source\_set\_enabled**(3)](/sd_event_source_set_enabled/3.md)

## NOTES

- 1\.

  Clean-up Variable Attribute

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