# How to clone a NIF resource

**URL:** <https://erlangforums.com/t/how-to-clone-a-nif-resource/1746>\
**Category:** Questions / Help\
**Tags:** nif\
**Created:** [August 16, 2022, 5:48pm UTC](https://erlangforums.com/t/how-to-clone-a-nif-resource/1746 "2022-08-16T17:48:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zabrane](https://erlangforums.com/letter_avatar_proxy/v4/letter/z/7ba0ec/32.png) [@zabrane](https://erlangforums.com/u/zabrane)\
**Post date:** [August 16, 2022, 5:48pm UTC](https://erlangforums.com/t/how-to-clone-a-nif-resource/1746/1 "2022-08-16T17:48:59Z")

</div>

Hi All,

I’m trying to clone a NIF resource like this:

```erlang

/* some code */

struct array_t *arr = (struct array_t *) enif_alloc_resource(ARRAY_RESOURCE, sizeof(struct array_t));

/* init the arr structure here */

ERL_NIF_TERM res1 = enif_make_resource(env, arr);
enif_release_resource(arr);

ERL_NIF_TERM res2 = enif_make_resource(env, arr);
enif_release_resource(arr);

return (enif_make_tuple(env, res1, res2));

```

But res1 and res2 are equal.  
What i’d like to achieve here it to get two different resources (res1 =/= res2) pointing to the same array\_t in memory.

Is that possible? If yes … how?

N.B: this is an experiement, please don’t tell me why 🙂

---

<div class="post-metadata">

**Author:** ![sverker](https://erlangforums.com/user_avatar/erlangforums.com/sverker/32/841_2.png) [@sverker](https://erlangforums.com/u/sverker)\
**Post date:** [August 18, 2022, 2:35pm UTC](https://erlangforums.com/t/how-to-clone-a-nif-resource/1746/2 "2022-08-18T14:35:05Z")

</div>

No, you cannot do that. The documentation says  
“Two resource terms will compare equal if and only if they would yield the same resource object pointer when passed to `enif_get_resource`”.

To get two different resources (res1=/=res2) you need to do two `enif_alloc_resource` calls.

---

<div class="post-metadata">

**Author:** ![sverker](https://erlangforums.com/user_avatar/erlangforums.com/sverker/32/841_2.png) [@sverker](https://erlangforums.com/u/sverker)\
**Post date:** [August 18, 2022, 2:43pm UTC](https://erlangforums.com/t/how-to-clone-a-nif-resource/1746/3 "2022-08-18T14:43:47Z")

</div>

You can of course solve it by introducing an indirection of your own. Two `enif_alloc_resource` blocks each with a pointer to the same array\_t allocated with `enif_alloc` for example. And then do your own memory management of that array\_t block with reference counting or whatever.

---

<div class="post-metadata">

**Author:** ![sverker](https://erlangforums.com/user_avatar/erlangforums.com/sverker/32/841_2.png) [@sverker](https://erlangforums.com/u/sverker)\
**Post date:** [August 18, 2022, 2:55pm UTC](https://erlangforums.com/t/how-to-clone-a-nif-resource/1746/4 "2022-08-18T14:55:19Z")

</div>

One more thing.  
Your code example is buggy. The second call to `enif_release_resource` is wrong.

“Each call to `enif_release_resource` must correspond to a previous call to `enif_alloc_resource` or `enif_keep_resource`. References made by `enif_make_resource` can only be removed by the garbage collector.”

If you run that code, you may get a premature deallocation of the resource while one of `res1` or `res2` is still referring it.

---

<div class="post-metadata">

**Author:** ![zabrane](https://erlangforums.com/letter_avatar_proxy/v4/letter/z/7ba0ec/32.png) [@zabrane](https://erlangforums.com/u/zabrane)\
**Post date:** [August 19, 2022, 10:26am UTC](https://erlangforums.com/t/how-to-clone-a-nif-resource/1746/5 "2022-08-19T10:26:40Z")

</div>

@sverker Thanks for the clarification
