Using unique_ptr in FreeRTOS I am trying to implement smart pointers for our project.
We're using FreeRTOS which is written with a C API so it was quite a challenge to implement sending smart pointers around but somehow I managed it by combining casts, new calls (dont really like that..) move semantics and some other tricks.

On the surface it behaves as expected, but I want to be sure there is not any memory leak.
Furthermore, is there a way to use make_unique together with new? I have not really found a way yet.

Here is the code, it should work with an online C++ compiler (or use this link). It would be great if an expert could look over this. Maybe some things can be optimized, or maybe this code is not save at all and I just dont see it.

// Example program
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;memory&gt;
#include &lt;cstring&gt;

class DummyHelper {
public:
    DummyHelper(uint8_t someResource): someResource(someResource) {}

    virtual ~DummyHelper() {
       someResource = 0;
       std::cout &lt;&lt; "Dtor called" &lt;&lt; std::endl;
    }

    int getResource() {
       return someResource;
    }
private:
   int someResource = 0; 
};

class CommandMessage {
public:
    CommandMessage() = default;
    virtual~ CommandMessage() {};

    void setParameter(uint64_t parameter) {
        memcpy(rawContent.data() + 4, &amp;parameter, sizeof(parameter));
    }

    uint64_t getParameter() const {
       uint64_t param;
       memcpy(&amp;param, rawContent.data() + 4, sizeof(param));
       return param;
    }

private:
    std::array&lt;uint8_t, 12&gt; rawContent;
};

void setTestMessage(CommandMessage *testMsg,
       uint8_t someResource) {
  auto unique_pptr = new std::unique_ptr&lt;DummyHelper&gt;(new DummyHelper(someResource));
  uint64_t test_ptr = reinterpret_cast&lt;uint64_t&gt;(unique_pptr);
  testMsg-&gt;setParameter(test_ptr);
}

std::unique_ptr&lt;DummyHelper&gt; getUniquePtr(
       CommandMessage *testMsg) {
   uint64_t raw_ptr = testMsg-&gt;getParameter();
   auto unique_pptr = reinterpret_cast&lt;std::unique_ptr&lt;DummyHelper&gt;*&gt;(raw_ptr);
   auto unique_ptr = std::move(*unique_pptr);
   delete unique_pptr;
   return unique_ptr;
}


int main()
{
  auto storagePtr = std::make_unique&lt;DummyHelper&gt;(42);
  CommandMessage testMessage;
  std::cout &lt;&lt; "Setting message" &lt;&lt; std::endl;
  setTestMessage(&amp;testMessage, 42);
  // a message has been set and can be sent, e.g. via a message queue

  // in another class, the pointer is retrieved.
  auto unique_ptr = getUniquePtr(&amp;testMessage);
  DummyHelper &amp; test = *unique_ptr;
  std::cout &lt;&lt; test.getResource() &lt;&lt; std::endl;
  unique_ptr.reset();
}
Overview:
In relation to the message queue:

Since you are doing a manual new and delete that seems a bit of a waste.
Also the only thing you are creating is a unique_ptr.
The unique_ptr is never in stored correctly typed so it never does any work.

So I would not store a pointer to a std::unique_ptr I would just store the original pointer. I like the idea of passing in and retrieving a unique_ptr to the access function.

Why are you passing a pointer to CommandMessage? It can't be null (you never check so must be valid. To make this more formal pass by reference to indicate to the user that it should be a normal object.
void setTestMessage(CommandMessage *testMsg, uint8_t someResource)
std::unique_ptr&lt;DummyHelper&gt; getUniquePtr(CommandMessage *testMsg)

// I would do this:

void setTestMessage(CommandMessage&amp; testMsg, uint8_t someResource)
std::unique_ptr&lt;DummyHelper&gt; getUniquePtr(CommandMessage&amp; testMsg)

Note: Putting the '*' on the right by the variable is very &quot;C&quot; like. C++ considers this part of the type information so it is usually on the left with the type.

Why is the input message different from the output message?
void setTestMessage(CommandMessage *testMsg, uint8_t someResource)
std::unique_ptr&lt;DummyHelper&gt; getUniquePtr(CommandMessage *testMsg)

So input is a uint8_t but the output is std::unique_ptr&lt;DummyHelper&gt;? I would expect the interface to be symmetric.
template&lt;typename T&gt;
void sendMessage(CommandMessage&amp; messageStream, std::unique_ptr&lt;T&gt;&amp;&amp; msg);

template&lt;typename T&gt;
std::unique_ptr&lt;T&gt; recvMessage(CommandMessage&amp; messageStream);


template&lt;typename T&gt;
std::unique_ptr&lt;T&gt; recvMessage(CommandMessage&amp; messageStream)
{
   // To store pointers in an integer type you should be using `std::intptr_t`
   // This is an int type that is guaranteed large enough to store a pointer.
   // Don't make the assumption that `uint64_t` is large enough.
   // It might be but that is not guaranteed for the future.
   std::intptr_t raw_ptr = messageStream.getParameter();

   return reinterpret_cast&lt;T*&gt;(raw_ptr);
}


template&lt;typename T&gt;
void sendMessage(CommandMessage&amp; messageStream, std::unique_ptr&lt;T&gt;&amp;&amp; msg)
{
    messageStream.setParameter(msg.release());
}