0

I would like to write a very very tiny matrix/tensor library with minimal features, and API like Eigen.

What confused me is, setting a debug break point, does not take me into some destructor function, I get nothing.

To reproduce, use this snippet that uses Eigen:

#include <Eigen/Dense>
#include <stdio.h>
#include <iostream>
using namespace Eigen;

int main()
{
    printf("hello Eigen\n");
    {
        MatrixXf m1(300, 400);
        MatrixXf m2(300, 400);
        MatrixXf m3 = m1 + m2;
        std::cout << m3.rows() << std::endl;
        printf("bye\n"); 
    }  // set a debug break point in this line, expecting going into its destructor and free memory, but there isn't
    printf("-----------------\n");

    return 0;
}

As a comparison, I simple created a Tensor template class, and when leaving its scope, its destructor is called:


template<class T>
class Tensor {
public:
    int c, h, w;
    int len;
    T* data;
    Tensor(int _c, int _h, int _w): c(_c), h(_h), w(_w) {
        len = c * h * w;
        data = new T[len];
    }
    ~Tensor() {
        if (data) {
            free(data);
        }
    }
};


int main() {

    // Tensor
    {
        printf("hello tensor\n");
        Tensor<float> tensor(3, 2, 2);
        printf("bye tensor\n");
    } // set a debug break point here, Tensor class's destructor is called
    
    return 0;
}

My question: when and where does Eigen's big matrix (with heap memory allocated) free its heap memory? And how can I see that?


update: I forget to mention that I was using Visual Studio 2019 Debug mode.

ChrisZZ
  • 1,521
  • 2
  • 17
  • 24
  • 1
    You may need a memory profiler:https://stackoverflow.com/questions/4690800/how-to-profile-memory-usage – prehistoricpenguin Apr 07 '21 at 03:44
  • 1
    if you want to put breakpoints on destruction i suggest you look at the disassembly view if you have one. you should be able to see the calls to the destructors in there – Borgleader Apr 07 '21 at 04:22
  • 1
    Even when compiled with just `-Og` or `-O1` the destructor will get optimized to a simple `call free`: https://godbolt.org/z/6obTY9G3W If you compile without any optimization at all, you may step trough the destruction process. – chtz Apr 07 '21 at 20:04
  • @prehistoricpenguin Thanks for providing this reference SO page, I didn't know the usage of valgrind with callgrind and KCachegrind until I see it. Tried compile under debug mode under Linux and KCachegrind shows calls like `aligned_free`. (But massif tool won't, LOL) – ChrisZZ Apr 08 '21 at 02:41
  • @Borgleader Thanks for providing this method. I was not familiar with disassembly and didn't know godbolt.org can easily see disassembly with Eigen this 3rdparty library. – ChrisZZ Apr 08 '21 at 02:43
  • @chtz Thanks for giving this straight-forward godbold page, I didn't know if it can use with Eigen library LOL, this is cool. And I tried using gdb/lldb under Linux (previously I was with Visual Studio 2019 x64 Debug mode), this time I can step into destructors like DenseStorage class. – ChrisZZ Apr 08 '21 at 02:46
  • The visual studio also has its memory profilers, you may give it a try:https://learn.microsoft.com/en-us/visualstudio/profiling/memory-usage?view=vs-2019, though you are using the same library on Windows and Linux, there are many differences between two platforms – prehistoricpenguin Apr 08 '21 at 03:05
  • @ChrisZZ you dont need godbolt for disassembly though you can just set a breakpoint in VS in your code and when it hits you can right click and "Go to Disassembly" (idk if theres a way to access it without having a breakpoint being triggered, thats usually when i use it anyway though) – Borgleader Apr 08 '21 at 03:09

1 Answers1

0

This is the compiler and debugger difference, after tried difference tools and methods from comments in this problem description.

The Visual Studio 2019 (until 2021-4-9, version 16.9.3) still can't debug step into DenseStorage class's destruction function.

While, Visual Studio 2019 with clang-cl, and lldb/gdb with clang-10/g++-9.3 on Linux, all can debug into DenseStorage class's destruction function.

ChrisZZ
  • 1,521
  • 2
  • 17
  • 24