title: segmentation fault 'gsl_spmatrix_add'
body: <p>EDIT:
  I have changed the question to new code that produces the same error and is more reliable in doing so.</p>
  <p>I have been struggling to find a segmentation fault in my code for a while now and have boiled it down to the following code:</p>
  <pre><code>#include &lt;gsl/gsl_spmatrix.h&gt;
  
  #include &lt;iostream&gt;
  
  using namespace std;
  
  void test_gsl() {
      size_t size = 5;
      size_t nzmax = 5 * 5;
      constexpr size_t threads = 5;
  
      // allocate
      gsl_spmatrix* thread_matrices[threads];
      for (size_t thread = 0; thread &lt; threads; thread++) {
          thread_matrices[thread] = gsl_spmatrix_alloc_nzmax(size, size, nzmax, GSL_SPMATRIX_TRIPLET);
      }
  
      // set
      for (size_t i = 0; i &lt; threads; i++) {
          gsl_spmatrix_set(thread_matrices[i], 0, 0, 1.0);
      }
  
      // crs
      for (size_t i = 0; i &lt; threads; i++) {
          gsl_spmatrix* temp = thread_matrices[i];
          thread_matrices[i] = gsl_spmatrix_crs(thread_matrices[i]);
          gsl_spmatrix_free(temp);
      }
  
      // add to total
      gsl_spmatrix* total_matrix = gsl_spmatrix_alloc_nzmax(size, size, nzmax, GSL_SPMATRIX_CRS);
      gsl_spmatrix* total_copy = gsl_spmatrix_alloc_nzmax(size, size, nzmax, GSL_SPMATRIX_CRS);
      for (size_t i = 0; i &lt; threads; i++) {
          gsl_spmatrix_memcpy(total_copy, total_matrix);  // this is required to avoid another segfault
          gsl_spmatrix_add(total_matrix, total_copy, thread_matrices[i]); // unknown segfault!
      }
  
      gsl_spmatrix_free(total_matrix);
      gsl_spmatrix_free(total_copy);
  }
  
  int main(int argc, char* argv[]) {
      
      test_gsl();
      printf(&quot;end\n&quot;);
  
      return 0;
  }
  
  </code></pre>
  <p>When I run this I consistently get the following output:</p>
  <pre><code>Segmentation fault (core dumped)
  </code></pre>
  <p>The segmentation fault is on the line with <code>gsl_spmatrix_add(total_matrix, total_copy, thread_matrices[i]);</code>.</p>
  <p>I'm compiling this code using cmake:</p>
  <pre><code>cmake_minimum_required(VERSION 3.22.1)
  
  project(diskmodel)
  
  set(CMAKE_CXX_STANDARD 14)
  set(CMAKE_CXX_STANDARD_REQUIRED YES)
  
  add_subdirectory(&quot;src&quot;)
  </code></pre>
  <pre><code>project(galaxy)
  
  find_package(GSL REQUIRED)
  
  add_executable(${PROJECT_NAME} main.cpp)
  
  set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME &quot;${PROJECT_NAME}&quot; SUFFIX &quot;.exe&quot;)
  
  target_link_libraries(${PROJECT_NAME} GSL::gsl GSL::gslcblas )
  </code></pre>
  <p><strong>What is causing this seg fault?</strong></p>
  <p>EDIT:</p>
  <p>After compiling with:
  <code>g++ 'gsl-config --libs' main.cpp -fsanitize=undefined -g</code> I get the same output as before. When compiling with <code>address</code> I get:</p>
  <pre><code>=================================================================
  ==31330==ERROR: LeakSanitizer: detected memory leaks
  
  Direct leak of 400 byte(s) in 5 object(s) allocated from:
      #0 0x7efd44b64a06 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:153
      #1 0x7efd449d393e in gsl_spmatrix_alloc_nzmax (/lib/x86_64-linux-gnu/libgsl.so.23+0x1f893e)
  
  Indirect leak of 240 byte(s) in 5 object(s) allocated from:
      #0 0x7efd44b64808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
      #1 0x7efd449d3b6c in gsl_spmatrix_alloc_nzmax (/lib/x86_64-linux-gnu/libgsl.so.23+0x1f8b6c)
  
  Indirect leak of 200 byte(s) in 5 object(s) allocated from:
      #0 0x7efd44b64808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
      #1 0x7efd449d3b88 in gsl_spmatrix_alloc_nzmax (/lib/x86_64-linux-gnu/libgsl.so.23+0x1f8b88)
  
  Indirect leak of 40 byte(s) in 5 object(s) allocated from:
      #0 0x7efd44b64808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
      #1 0x7efd449d39ac in gsl_spmatrix_alloc_nzmax (/lib/x86_64-linux-gnu/libgsl.so.23+0x1f89ac)
  
  Indirect leak of 40 byte(s) in 5 object(s) allocated from:
      #0 0x7efd44b64808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
      #1 0x7efd449d397d in gsl_spmatrix_alloc_nzmax (/lib/x86_64-linux-gnu/libgsl.so.23+0x1f897d)
  </code></pre>
  <p>When compiling using my cmake file and running <code>gdb galaxy.exe</code> I get the following backtrace:</p>
  <pre><code>#0  0x00007ffff7f2c185 in gsl_spblas_scatter () from /lib/x86_64-linux-gnu/libgsl.so.23
  #1  0x00007ffff7f2b364 in gsl_spmatrix_add () from /lib/x86_64-linux-gnu/libgsl.so.23
  #2  0x00005555555553d2 in test_gsl () at .../src/main.cpp:35
  #3  0x0000555555555420 in main (argc=1, argv=0x7fffffffdaf8) at .../src/main.cpp:44
  </code></pre>
  <p>and no history when using <code>-p</code>.</p>
  <p>When using <code>ulimit -c unlimited</code> and then running a core file is not generated. I tried looking into this, but I can't seem to find it to generate anywhere and I don't know why.</p>