igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] possible attributes bug


From: Gábor Csárdi
Subject: Re: [igraph] possible attributes bug
Date: Sat, 23 Oct 2010 12:16:25 +0200

Hi Marco,

this is indeed a bug, actually not in the attribute handling, but in
igraph_strvector, or maybe even in libc. realloc() should work find
with size=0 AFAIK, but it does not. Anyway, I have corrected this, and
a patch is attached.

Thanks for the detailed report!

Best,
Gabor

=== modified file 'src/igraph_strvector.c'
--- src/igraph_strvector.c      2008-09-20 16:34:35 +0000
+++ src/igraph_strvector.c      2010-10-23 10:01:15 +0000
@@ -394,17 +394,17 @@
 int igraph_strvector_resize(igraph_strvector_t* v, long int newsize) {
   long int toadd=newsize-v->len, i, j;
   char **tmp;
+  long int reallocsize=newsize;
+  if (reallocsize==0) { reallocsize=1; }

   assert(v != 0);
   assert(v->data != 0);
 /*   printf("resize %li to %li\n", v->len, newsize); */
   if (newsize < v->len) {
-    long int i, reallocsize=newsize;
     for (i=newsize; i<v->len; i++) {
       igraph_Free(v->data[i]);
     }
     /* try to give back some space */
-    if (reallocsize==0) { reallocsize=1; }
     tmp=igraph_Realloc(v->data, reallocsize, char*);
 /*     printf("resize %li to %li, %p\n", v->len, newsize, tmp); */
     if (tmp != 0) {
@@ -412,7 +412,7 @@
     }
   } else if (newsize > v->len) {
     igraph_bool_t error=0;
-    tmp=igraph_Realloc(v->data, newsize, char*);
+    tmp=igraph_Realloc(v->data, reallocsize, char*);
     if (tmp==0) {
       IGRAPH_ERROR("cannot resize string vector", IGRAPH_ENOMEM);
     }
@@ -516,8 +516,8 @@
       igraph_Free(v->data[i]);
     }
   }
-  /* Try to make it shorter */
-  tmp=igraph_Realloc(v->data, v->len-nremove, char*);
+  /* Try to make it shorter */
+  tmp=igraph_Realloc(v->data, v->len-nremove ? v->len-nremove : 1, char*);
   if (tmp != 0) {
     v->data=tmp;
   }
@@ -544,7 +544,7 @@
     }
   }
   /* Try to give back some memory */
-  tmp=igraph_Realloc(v->data, v->len-nremove, char*);
+  tmp=igraph_Realloc(v->data, v->len-nremove ? v->len-nremove : 1, char*);
   if (tmp != 0) {
     v->data=tmp;
   }


On Fri, Oct 22, 2010 at 7:42 PM, Marco Valerio Barbera
<address@hidden> wrote:
> Hello,
>
> I've encountered a strange behaviour using igraph nodes attributes on
> a C toy-program.
>
> test.cpp:
>
> #include <stdlib.h>
> #include <igraph/igraph.h>
>
> int main(void) {
>
>        igraph_t G;
>
>        igraph_i_set_attribute_table(&igraph_cattribute_table);
>
>        if(igraph_empty(&G, 2, false) != 0)
>                abort();
>
>        if(igraph_cattribute_VAS_set(&G, "name", 0, "0") != 0)
>                abort();
>        if(igraph_cattribute_VAS_set(&G, "name", 1, "1") != 0)
>                abort();
>
>        if(igraph_delete_vertices(&G, igraph_vss_1(0)) != 0)
>                abort();
>        if(igraph_delete_vertices(&G, igraph_vss_1(0)) != 0)
>                abort();
>
>        igraph_destroy(&G);
>
>        return 0;
> }
>
> If I compile this program using a simple line like
>
> g++ test.cpp -ligraph
>
> and run it then everything goes fine.
> However, if you compile with
>
> g++ test.cpp -ligraph -lmcheck
>
> when I run the program I get a "block freed twice" error message when
> igraph_destroy() is called and the program aborts
> For those who don't know what the -lmcheck does, it is a GNU extension
> that does heap consistency check every time a malloc/free happens
> (ref: 
> http://www.gnu.org/s/libc/manual/html_node/Heap-Consistency-Checking.html)
>
> After studying better the issue I noticed that this problem does NOT
> happen if you do EITHER ONE of the following things:
>
> 1 - comment the igraph_cattribute_VAS_() calls (i.e. don't attach
> attributes to nodes but still call the igraph_i_set_attridute_table()
> function)
> 2 - don't delete both the nodes in the graph (i.e. the graph has at
> least still one vertex before it is delted)
>
> the memory error reported by the consistency check tool has to be
> taken seriously since in a bigger program I'm writing, not compiling
> with -lmcheck makes the program go past the igraph_destroy() call but
> produces a segm. fault after a few malloc allocation calls.
>
> by the way, running the program with valgrind produces an error too.
>
> I don't know if this strange behaviour is caused by a bug in the
> igraph library or if it's me doing something wrong handling the
> attributes.
>
> could you please help me?
>
> thanks
>
> mb.
>
> p.s. I add some information that might be useful to understand the behaviour.
>
> os: ubuntu server 64 bit
> g++ version: g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
> igraph version: both 0.5.3 and 0.5.4 (the first one is the package
> that comes from the ubuntu server repository, while the second has
> been manually downloaded and compiled. Both the versions show the same
> behaviour)
>
> thanks again
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>



-- 
Gabor Csardi <address@hidden>     UNIL DGM

Attachment: strvector.patch
Description: Binary data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]