[ 
https://issues.apache.org/jira/browse/AVRO-1361?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13752428#comment-13752428
 ] 

Douglas Creager commented on AVRO-1361:
---------------------------------------

The main issue is how you're filling in the contents of the map.  Your code 
assumes that you need to create an avro_value_t for the value of each entry, 
and then use avro_value_add to add that value that you created to the map.  
That's actually not the right approach.  Instead, the map is responsible for 
managing the storage of the values that it contains.  So instead of creating a 
value, and then calling avro_value_add, you just call avro_value_add.  The map 
implementation will allocate a new value for the given key (or return an 
existing value if the key is already present).  You can then use 
avro_value_set_string to fill in that value that the map created for you.

On a related note, when you extract the key and value for each map entry, your 
code allocates a char buffer.  You don't need to do this.  The map value, and 
the string values that it contains, have their own copies of the actual string 
content.  avro_value_get_by_index and avro_value_get_string don't copy this 
content; instead, they return a pointer to that storage that's managed by the 
value instance.

There are more details in ยง4.1.3 of the [C 
documentation|http://avro.apache.org/docs/current/api/c/index.html#_avro_values].
  I'm also going to attach a modified version of your main.c, which produces 
the right results (and has no memory leaks).  You can diff the two to see 
exactly what changes were needed, and I'm happy to discuss any of the changes 
in more detail if you like.
                
> The values of a map are not correct
> -----------------------------------
>
>                 Key: AVRO-1361
>                 URL: https://issues.apache.org/jira/browse/AVRO-1361
>             Project: Avro
>          Issue Type: Bug
>          Components: c
>    Affects Versions: 1.7.4
>         Environment: Ubuntu 12.10
>            Reporter: Tibor Benke
>         Attachments: main.c, main-fixed.c
>
>
> When I put values into a map, the values appear delayed with one cycle:
> // pseudo code
> map = Map()
> for (i = 0; i < 5; i++) {
>    map.put(i, i)
> }
> // then map contains the following elements: {"0": "1", "1": "2", "2": "3", 
> "3": "4", "4": ""} 
> I wrote a sample code, which demonstrates the bug. I hope I'm not wrong. I 
> have also problems with the reference counting: if I comment out the free() 
> calls in the program, I get glibc errors. Is it possible, that the Avro frees 
> not just its own pointers in the *decref() calls?

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to