Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Code Segfaults #2

Open
bhonecode opened this issue Jun 3, 2016 · 0 comments
Open

Example Code Segfaults #2

bhonecode opened this issue Jun 3, 2016 · 0 comments

Comments

@bhonecode
Copy link

Example Code from your readme segfaults on vc_vector_release. Checked on Clang / OS X and GCC / Ubuntu 16.04.

Code Below:

#include <stdlib.h>                                                             
#include "vc_vector.h"                                                          

struct Item {                                                                   
  int val1;                                                                     
  int val2;                                                                     
};                                                                              

int main() {                                                                    
  const int n = 10;                                                             

  // Creates an empty vector with the reserved size for the 'n' elements        
  // and with custom deleter 'free'. Vector will contain pointers to 'Item'     
  vc_vector* v = vc_vector_create(n, sizeof(struct Node*), free);               
  if (!v) {                                                                     
    return 1;                                                                   
  }                                                                             

  struct Item* item = NULL;                                                     
  const int count = n + 1;                                                      
  // Vector automatically increases the reserved size when 'n + 1' will be added
  for (int i = 0; i < count; ++i) {                                             
    // Creating item                                                            
    item = (struct Item *) malloc(sizeof(struct Item));                         
    if (!item) {                                                                
      continue;                                                                 
    }                                                                           

    item->val1 = i;                                                             
    item->val2 = 0;                                                             

    // Pushing to the end of the vector                                         
    if (!vc_vector_push_back(v, item)) {                                        
      // If the item was not pushed, you have to delete it                      
      printf( "not pushed\n");                                                  
      free(item);                                                               
    }                                                                           
  }                                                                             

  // ...                                                                        

  // Calls custom deleter 'free' for all items                                  
  // and releases the vector                                                    
  vc_vector_release(v);                                                         
  return 0;                                                                     
}


Valgrind Output:

==15217== Memcheck, a memory error detector
==15217== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15217== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==15217== Command: ./test2
==15217== 
==15217== Invalid free() / delete / delete[] / realloc()
==15217==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217==    by 0x40086D: vc_vector_call_deleter (vc_vector.c:40)
==15217==    by 0x4008B0: vc_vector_call_deleter_all (vc_vector.c:45)
==15217==    by 0x400A2D: vc_vector_release (vc_vector.c:96)
==15217==    by 0x4007BE: main (test2.c:44)
==15217==  Address 0x52034b8 is 8 bytes inside a block of size 120 free'd
==15217==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217==    by 0x40086D: vc_vector_call_deleter (vc_vector.c:40)
==15217==    by 0x4008B0: vc_vector_call_deleter_all (vc_vector.c:45)
==15217==    by 0x400A2D: vc_vector_release (vc_vector.c:96)
==15217==    by 0x4007BE: main (test2.c:44)
==15217==  Block was alloc'd at
==15217==    at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217==    by 0x4007FD: vc_vector_realloc (vc_vector.c:27)
==15217==    by 0x401154: vc_vector_append (vc_vector.c:279)
==15217==    by 0x4011E5: vc_vector_push_back (vc_vector.c:295)
==15217==    by 0x400786: main (test2.c:33)
==15217== 
==15217== Invalid free() / delete / delete[] / realloc()
==15217==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217==    by 0x400A4A: vc_vector_release (vc_vector.c:100)
==15217==    by 0x4007BE: main (test2.c:44)
==15217==  Address 0x52034b0 is 0 bytes inside a block of size 120 free'd
==15217==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217==    by 0x40086D: vc_vector_call_deleter (vc_vector.c:40)
==15217==    by 0x4008B0: vc_vector_call_deleter_all (vc_vector.c:45)
==15217==    by 0x400A2D: vc_vector_release (vc_vector.c:96)
==15217==    by 0x4007BE: main (test2.c:44)
==15217==  Block was alloc'd at
==15217==    at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217==    by 0x4007FD: vc_vector_realloc (vc_vector.c:27)
==15217==    by 0x401154: vc_vector_append (vc_vector.c:279)
==15217==    by 0x4011E5: vc_vector_push_back (vc_vector.c:295)
==15217==    by 0x400786: main (test2.c:33)
==15217== 
==15217== 
==15217== HEAP SUMMARY:
==15217==     in use at exit: 88 bytes in 11 blocks
==15217==   total heap usage: 14 allocs, 14 frees, 328 bytes allocated
==15217== 
==15217== LEAK SUMMARY:
==15217==    definitely lost: 88 bytes in 11 blocks
==15217==    indirectly lost: 0 bytes in 0 blocks
==15217==      possibly lost: 0 bytes in 0 blocks
==15217==    still reachable: 0 bytes in 0 blocks
==15217==         suppressed: 0 bytes in 0 blocks
==15217== Rerun with --leak-check=full to see details of leaked memory
==15217== 
==15217== For counts of detected and suppressed errors, rerun with: -v
==15217== ERROR SUMMARY: 11 errors from 2 contexts (suppressed: 0 from 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant