memory leak about hyperdex client

49 views
Skip to first unread message

李俊良

unread,
Feb 18, 2016, 4:26:54 AM2/18/16
to hyperdex-discuss
the search function, the example code like this:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <hyperdex/admin.hpp>
#include <hyperdex/client.hpp>
#include <hyperdex/client.h>
#include <e/endian.h>

using namespace std;

int
main
(int argc, const char *argv[])
{
   
struct hyperdex_client *client = NULL;
   
struct hyperdex_client_attribute attr;
   
struct hyperdex_client_attribute_check check;
   
const struct hyperdex_client_attribute *attrs = NULL;
    size_t attrs_sz
= 0;
    size_t check_len
= 0;
    uint64_t count
;
    int64_t op_id
;
   
enum hyperdex_client_returncode op_status;
    int64_t loop_id
;
   
enum hyperdex_client_returncode loop_status;
    size_t i
;
   
const char *ns = "phonebook";
    client
= hyperdex_client_create("127.0.0.1", 1982);
    check
.attr = "first";
    check
.value = "Jack";
    check
.value_sz = strlen(check.value);
    check
.datatype = HYPERDATATYPE_STRING;
    check
.predicate = HYPERPREDICATE_EQUALS;
    op_id
= hyperdex_client_search(client, ns, &check, 1, &op_status, &attrs, &attrs_sz);

   
if (op_id < 0)
   
{
        cerr
<< "search failed: " << hyperdex_client_error_message(client) << endl;
        hyperdex_client_destroy
(client);
       
return EXIT_FAILURE;
   
}

   
while (true)
   
{
        loop_id
= hyperdex_client_loop(client, -1, &loop_status);

       
if (loop_id < 0)
       
{
            cerr
<< hyperdex_client_error_message(client) << endl;
           
break;
       
}

       
assert(op_id == loop_id);

       
if (loop_status == HYPERDEX_CLIENT_SUCCESS)
       
{
            printf
("get done(%d)\n", attrs_sz);

           
for (i = 0; i < attrs_sz; ++i)
           
{
               
switch (attrs[i].datatype)
               
{
               
case HYPERDATATYPE_STRING:
                    printf
("got attribute \"%s\" = \"%.*s\"\n", attrs[i].attr, attrs[i].value_sz, attrs[i].value);
                   
break;

               
case HYPERDATATYPE_INT64:
               
{
                    uint64_t num
= 0;
                    e
::unpack64le(attrs[i].value, &num);
                    printf
("got attribute \"%s\" = \"%lu\"\n", attrs[i].attr, num);
               
}
               
break;

               
case HYPERDATATYPE_FLOAT:
               
{
                   
double num = 0;
                    e
::unpackdoublele(attrs[i].value, &num);
                    printf
("got attribute \"%s\" = \"%f\"\n", attrs[i].attr, num);
               
}
               
break;

               
default:
                    printf
("got error attribute\n");
               
}
           
}
       
}
   
}

    hyperdex_client_destroy_attrs
(attrs, attrs_sz);
    hyperdex_client_destroy
(client);
   
return EXIT_SUCCESS;


the m_attrs  in loop function , each cycle and malloc once, i have fixed like this :
--- a/client/util.cc
+++ b/client/util.cc
@@ -88,9 +88,20 @@ hyperdex :: value_to_attributes(const configuration& config,
 
     std
::vector<hyperdex_client_attribute> ha;
     ha
.reserve(sc->attrs_sz);
-    char* ret = static_cast<char*>(malloc(sz));
+       char *ret = NULL;
+       if (attrs)
+       {
+        if (*attrs == NULL)
+        {
+            ret = static_cast<char*>(malloc(sz));
+        }
+               else
+               {
+            ret = static_cast<char*>(realloc((void *)*attrs, sz));
+               }
+       }
 
-    if (!ret)
+    if (ret == NULL)




李俊良

unread,
Feb 18, 2016, 4:32:24 AM2/18/16
to hyperdex-discuss
Additional, the result of sorted_search is wrong:
[cnangel@localhost hyperdex-1.8.2]$python
Python 2.7.10 (default, Sep  8 2015, 17:20:17)
[GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hyperdex.client
>>> c = hyperdex.client.Client('127.0.0.1', 1982)
>>> [x for x in c.search('phonebook', {'first': 'Jack'})]
[{'username': 'jsmith2', 'phone': 6075551024L, 'last': 'Smith', 'first': 'Jack'}, {'username': 'jsmith2018', 'phone': 6085551024L, 'last': 'Smith', 'first': 'Jack'}, {'username': 'jsmith3188', 'phone': 6075551024L, 'last': 'Smith', 'first': 'Jack'}]
>>> [x for x in c.sorted_search('phonebook', {'first': 'Jack'})]
Traceback (most recent call last):
 
File "<stdin>", line 1, in <module>
 
File "hyperdex/client.pyx", line 2408, in hyperdex.client.Client.sorted_search (bindings/python/hyperdex/client.c:65509)
TypeError: sorted_search() takes exactly 5 positional arguments (2 given)
>>> [x for x in c.sorted_search('phonebook', {'first': 'Jack'}, 'first', 2, 'max')]
[{'username': 'jsmith2018', 'phone': 6085551024L, 'last': 'Smith', 'first': 'Jack'}, {'username': '\x00\x08\x00\x00\x00\x00\x00\x00\x00@', 'phone': 35789034766692096L, 'last': '\x00\x00\x05\x00\x00', 'first': '\x00\x00f\x1f'}]




在 2016年2月18日星期四 UTC+8下午5:26:54,李俊良写道:
...

liangd...@gmail.com

unread,
Sep 19, 2016, 3:30:37 AM9/19/16
to hyperdex-discuss
I noticed you changed code  often. I just wonder if you are using hyperdex in production environment? 


在 2016年2月18日星期四 UTC+8下午5:26:54,李俊良写道:
the search function, the example code like this:

Robert Escriva

unread,
Sep 19, 2016, 9:33:13 AM9/19/16
to hyperdex...@googlegroups.com
The leak is because you are not freeing each of the returned values.
This is documented in the C API here:
http://hyperdex.org/doc/latest/CClientAPI/#api:c:search

-Robert
> --
> You received this message because you are subscribed to the Google Groups
> "hyperdex-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to hyperdex-discu...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages