mardi 29 janvier 2019

C++ bad_alloc exception on future.get

My C++ program is exiting with a bad_alloc exception. I'm trying to find the cause, but don't quite know how to debug these kind of exceptions.

So far, I compiled the program in debug mode, run it with gdb and set a breakpoint before throwing the exception (b 'std::bad_alloc::bad_alloc()').

After the exception was thrown, I inspected the stack (bt), which showed the following:

(gdb) bt
#0  0x00007ffff752f1f7 in raise () from /lib64/libc.so.6
#1  0x00007ffff75308e8 in abort () from /lib64/libc.so.6
#2  0x00007ffff7f0a7fd in __gnu_cxx::__verbose_terminate_handler () at ../../../../libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7f08876 in __cxxabiv1::__terminate (handler=<optimized out>) at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:47
#4  0x00007ffff7f088c1 in std::terminate () at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:57
#5  0x00007ffff7f0886a in std::rethrow_exception (ep=...) at ../../../../libstdc++-v3/libsupc++/eh_ptr.cc:259
#6  0x0000000000419001 in std::__basic_future<void>::_M_get_result (this=0x55b9a0)
    at /soft/EB_repo/devel/programs/foss/2016b/GCCcore/5.4.0/include/c++/5.4.0/future:683
#7  0x0000000000416da6 in std::future<void>::get (this=0x55b9a0) at /soft/EB_repo/devel/programs/foss/2016b/GCCcore/5.4.0/include/c++/5.4.0/future:846
#8  0x00000000004d152f in cluster_reads (reads=..., kmer_size=14, t_s=0.10000000000000001, t_v=500, bv_threshold=0.40000000000000002, 
    min_bv_threshold=0.20000000000000001, bv_falloff=0.050000000000000003, min_reads_cluster=0, n_threads=8) at cluster.cpp:81
#9  0x000000000040910b in main (argc=8, argv=0x7fffffffc028) at main.cpp:106

As you can see in #7, the exception is being thrown when getting the std::future? Here is the piece of code from cluster_reads (#8) that is crashing (I marked line 81 with an arrow).

    std::vector<std::vector<kmer_t>> kmers(reads.size());
    std::vector<std::vector<kmer_t>> rev_kmers(reads.size());

    std::vector<kmer_bv_t> bv_kmers(reads.size());
    std::vector<kmer_bv_t> rev_bv_kmers(reads.size());

    std::vector<std::future<void>> tasks;
    for (int t = 0; t < n_threads; ++t) {
        tasks.emplace_back(std::async(std::launch::async, [t, &reads, n_threads, kmer_size, &kmers, &rev_kmers, &bv_kmers, &rev_bv_kmers] {
            for (int i = t; i < reads.size(); i+=n_threads) {
                read_kmers_t k1 = extract_kmers_from_read(reads[i].seq, kmer_size);

                kmers[i] = k1.list_forward;
                rev_kmers[i] = k1.list_reverse;
                bv_kmers[i] = k1.bv_forward;
                rev_bv_kmers[i] = k1.bv_reverse;
            }
        }));
    }

    for (auto &&task : tasks) {
        task.get(); <------------------- line 81
    }

How can I debug further this issue? I'm pretty new to gdb. Any idea on what might be causing the bad_alloc exception?

Aucun commentaire:

Enregistrer un commentaire