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.