EDIT: I have changed the question to new code that produces the same error and is more reliable in doing so.
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:
#include <gsl/gsl_spmatrix.h>
#include <iostream>
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 < threads; thread++) {
thread_matrices[thread] = gsl_spmatrix_alloc_nzmax(size, size, nzmax, GSL_SPMATRIX_TRIPLET);
}
// set
for (size_t i = 0; i < threads; i++) {
gsl_spmatrix_set(thread_matrices[i], 0, 0, 1.0);
}
// crs
for (size_t i = 0; i < 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 < 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("end\n");
return 0;
}
When I run this I consistently get the following output:
Segmentation fault (core dumped)
The segmentation fault is on the line with gsl_spmatrix_add(total_matrix, total_copy, thread_matrices[i]);.
I'm compiling this code using cmake:
cmake_minimum_required(VERSION 3.22.1)
project(diskmodel)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
add_subdirectory("src")
project(galaxy)
find_package(GSL REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}" SUFFIX ".exe")
target_link_libraries(${PROJECT_NAME} GSL::gsl GSL::gslcblas )
What is causing this seg fault?
EDIT:
After compiling with:
g++ 'gsl-config --libs' main.cpp -fsanitize=undefined -g I get the same output as before. When compiling with address I get:
=================================================================
==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)
When compiling using my cmake file and running gdb galaxy.exe I get the following backtrace:
#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
and no history when using -p.
When using ulimit -c unlimited 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.