# Should I free the objects made by C functions?

**URL:** <https://forum.crystal-lang.org/t/should-i-free-the-objects-made-by-c-functions/266>\
**Category:** Help & Support\
**Created:** [January 10, 2019, 6:02am UTC](https://forum.crystal-lang.org/t/should-i-free-the-objects-made-by-c-functions/266 "2019-01-10T06:02:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/arcage/32/1250_2.png) [@arcage](https://forum.crystal-lang.org/u/arcage)\
**Post date:** [January 10, 2019, 6:02am UTC](https://forum.crystal-lang.org/t/should-i-free-the-objects-made-by-c-functions/266/1 "2019-01-10T06:02:31Z")

</div>

I have used **getifaddrs** () function of libc from my [crystal code](https://github.com/arcage/net_sample.cr).

And, I found following line in the [getifaddrs(3) man page](http://man7.org/linux/man-pages/man3/getifaddrs.3.html)

> The data returned by **getifaddrs** () is dynamically allocated and should be freed using **freeifaddrs** () when no longer needed.

In these case, should I manually free the objects that dynamically allocated by C functions?

Or, crystal GC will automatically collect them?

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [January 10, 2019, 7:12am UTC](https://forum.crystal-lang.org/t/should-i-free-the-objects-made-by-c-functions/266/2 "2019-01-10T07:12:39Z")

</div>

You will need to manually free the resource as you would in C. You can wrap the object in a class to get the GC to free it for you:

```crystal
class IfAddrs
  @data : Pointer(SomeType)
  def initialize
    @data = LibYourLib.getifaddrs
     # OR?
    @data = Pointer(SomeType).null
    LibYourLib.getifaddrs @data
  end
  def finalize
    LibYourLib.freeifaddrs @obj
  end
end

```

You should also bear in mind that the Pointer type is not null-safe and you should be checking if `@data` is null before every use:

```crystal
class IfAddrs
  ....
  def data?
    @data
  end
  def data
    if not_nil_data = @data
      return not_nil_data
    else
      raise NullPointerException.new
    end
  end
  ...
end

```

---

<div class="post-metadata">

**Author:** ![arcage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/arcage/32/1250_2.png) [@arcage](https://forum.crystal-lang.org/u/arcage)\
**Post date:** [January 10, 2019, 8:36am UTC](https://forum.crystal-lang.org/t/should-i-free-the-objects-made-by-c-functions/266/3 "2019-01-10T08:36:55Z")

</div>

Thank you for your response.

I got it.

I will try to implement freeing those objects.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [January 10, 2019, 10:35am UTC](https://forum.crystal-lang.org/t/should-i-free-the-objects-made-by-c-functions/266/4 "2019-01-10T10:35:33Z")

</div>

You should probably also use [out](https://crystal-lang.org/reference/syntax_and_semantics/c_bindings/out.html) to simplify the code, and checking a `null` pointer is not needed because `getifaddrs` returns `-1` on failure.

---

<div class="post-metadata">

**Author:** ![arcage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/arcage/32/1250_2.png) [@arcage](https://forum.crystal-lang.org/u/arcage)\
**Post date:** [January 11, 2019, 2:05am UTC](https://forum.crystal-lang.org/t/should-i-free-the-objects-made-by-c-functions/266/5 "2019-01-11T02:05:36Z")

</div>

Thanks for all.

I updated my repo.

1. By using freeifaddrs() function, I confirmed that memory usage was not increased by multiple getifaddrs() calls.

2. I added error check(return value check) when calling getifaddrs().

3. I replaced some variable definitions to using **out** keyword.
