So the following loop:<div><br><div><span style="font-family:'courier new',monospace"> for (int k = 0; <font>k</font> < table.num_entries; k++) {<br><b> free(table.entries[k].value.value.array.entries);</b><br>
}</span></div><div><font face="courier new, monospace"><br></font>This would only work if table.entries[k].kind == AMQP_FIELD_KIND_ARRAY. If kind is something else, you get undefined behavior, which is what you're getting.</div>
<div><br></div><div>-Alan</div><div><br><br><div class="gmail_quote">On Mon, Nov 5, 2012 at 10:31 AM, Arun Chandrasekaran <span dir="ltr"><<a href="mailto:visionofarun@gmail.com" target="_blank">visionofarun@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br><br><font><font>Has anyone used rabbitmq-c with high availability configuration?<br><br></font></font>I am stuck with a double free corruption when using rabbitmq-c with high availability configuration. For HA, since the nodes names have to be taken from a config file, I have implemented the following way of getting the key value params.<br>
<br><font><font face="tahoma,sans-serif"><font><font><font><font><font><font><font><font><font><span style="font-family:courier new,monospace"><font> amqp_table_t table;<br> std::vector<amqp_field_value_t_ *> array_ptrs;<br>
<br> memset(&table, 0, sizeof(table));<br> table.num_entries = ha_map.size();<br> if (table.num_entries)<br> table.entries = (amqp_table_entry_t *) calloc(1, sizeof(amqp_table_entry_t) * table.num_entries);<br>
int i = 0;<br><br> std::map<std::string, std::vector<std::string> >::const_iterator it;<br> for (it = ha_map.begin(); it != ha_map.end(); it++, i++) {<br> table.entries[i].key.len = (*it).first.size();<br>
table.entries[i].key.bytes = (void *) (*it).first.c_str();<br><br> if ((*it).second.size() == 1) {<br> table.entries[i].value.kind = AMQP_FIELD_KIND_UTF8;<br> table.entries[i].value.value.bytes.len = (*it).second[0].size();<br>
table.entries[i].value.value.bytes.bytes = (void *) (*it).second[0].c_str();<br><br> } else if ((*it).second.size() > 1) {<br> table.entries[i].value.kind = AMQP_FIELD_KIND_ARRAY;<br> table.entries[i].value.value.array.num_entries = (*it).second.size();<br>
amqp_field_value_t *f = (amqp_field_value_t_ *) malloc(sizeof(amqp_field_value_t_) * (*it).second.size());<br> array_ptrs.push_back(f);<br> table.entries[i].value.value.array.entries = f;<br> LogInfo(("Before pointer: %p", table.entries[i].value.value.array.entries));<br>
for (size_t j = 0; j < (*it).second.size(); j++) {<br> table.entries[i].value.value.array.entries[j].kind = AMQP_FIELD_KIND_UTF8;<br> table.entries[i].value.value.array.entries[j].value.bytes.len = (*it).second[j].size();<br>
table.entries[i].value.value.array.entries[j].value.bytes.bytes = (void *) (*it).second[j].c_str();<br> }<br> LogInfo(("Before1 pointer: %p", table.entries[i].value.value.array.entries));<br>
}<br> }<br><br> amqp_queue_declare_ok_t *r =<br> amqp_queue_declare(m_conn, m_channel, amqp_cstring_bytes(m_queue.c_str()), 0, 1, 0, 0, table);<br><br> for (size_t i = 0; i < array_ptrs.size(); i++) {<br>
free(array_ptrs[i]);<br> }<br> free(table.entries);<br></font><br><font><font face="tahoma,sans-serif">This works perfectly fine. But the <font>crash happens when I <font>replace<br></font></font></font></font></span></font></font></font></font></font></font></font></font></font></font><br>
<font><font face="tahoma,sans-serif"><font><font><font><font><font><font><font><font><font><span style="font-family:courier new,monospace"><font><font face="tahoma,sans-serif"><font><font><font><font face="tahoma,sans-serif"><font><font><font><font><font><font><font><font><font><span style="font-family:courier new,monospace"><font><font><font face="tahoma,sans-serif"><font><font><font><font><font><font><font><font><font><span style="font-family:courier new,monospace"><font> for (size_t i = 0; i < array_ptrs.size(); i++) {<br>
free(array_ptrs[i]);<br> }<br></font><br></span></font></font></font></font></font></font></font></font></font></font></font></font></span></font></font></font></font></font></font></font></font></font></font></font></font></font></font></font></span><font><font face="tahoma,sans-serif">with<br>
</font></font><font><span style="font-family:courier new,monospace"> for (int k = 0; <font>k</font> < table.num_entries; k++) {<br><b> free(table.entries[k].value.value.array.entries);</b><br> }</span><br><br>
<font>Am<font> I doing something wrong here? Or <font>i</font></font>s this a bug in the rabbitm<font>q-c library itself?</font></font><br><br><font>Thanks,<br><font>Arun</font><br></font>
<br>_______________________________________________<br>
rabbitmq-discuss mailing list<br>
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a><br>
<a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br>
<br></font></font></font></font></font></font></font></font></font></font></font></font></font></blockquote></div><br></div></div>