c++hiredis

Writing a safe wrapper class for memory management of a user-defined object's pointer


I'm using redis c++ client to develop client APIs which will do CRUD operations on the redis cluster. The client library that I'm using returns a pointer of a redisReply structure whenever a command is executed. Later, I'm expected to use the freeReplyObject() function on the pointer to free the memory.

In order to develop a safer way, so that I inadvertently don't use freeReplyObject() on an invalid pointer, I'm thinking about writing a wrapper class:

class reply_wrapper
{
        public:
                redisReply* p_reply;
                reply_wrapper(redisReply* reply = NULL)
                {
                        p_reply=reply;
                }

                ~reply_wrapper()
                {
                        freeReplyObject(p_reply);
                }
};

I'm thinking about constructing an object of this class, whenever I will execute a command on the server. I think by doing this I no longer will have to free the memory manually. Is this approach correct here and is there a better approach?

Please note that freeReplyObject() handles the case of null pointer.


I'm getting almost all the suggestions about using shared-pointer/unique-pointer. While I check the examples available online and see how it fits my scenario(custom destructor), I would also like to know if there's anything fundamentally wrong in my method of handling the memory above.



Solution

  • As suggested, I've used std::unique_ptr and while constructing it I had to pass a functor which calls the freeReplyObject function in it.

    struct redisReplyDeleterFunctor {
          void operator()(redisReply* p) {
              freeReplyObject(p);
          }
      };
    
    unique_ptr<redisReply, redisReplyDeleterFunctor> reply((redisReply*)(redisClusterCommand(getDbContext(),  command.c_str())));
    

    Now I don't need to call the freeReplyObject() manually and the memory will be automatically released as soon as my unique_ptr goes out of scope.