LibTorch: double free or corruption (!prev) in VariableVersion

MathBunny: <p>Hello, I am trying to run a code snippet of LibTorch and I am running into a segmentation fault.</p>
<h2>
<a name="background" class="anchor" href="#background"></a>Background</h2>
<p>I am building and linking using Gradle, with <code>*.so</code> files created from source. The project successfully links and compiles. However, when I run my unit tests I get a segmentation fault in the destructor of what appears to be <code>VariableVersion</code>.</p>
<h2>
<a name="example-failure" class="anchor" href="#example-failure"></a>Example Failure</h2>
<p>This is my code currently:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;gflags/gflags.h&gt;
#include &lt;glog/logging.h&gt;
#include &lt;gmock/gmock.h&gt;
#include &lt;gtest/gtest.h&gt;

class pytorch_test : public ::testing::Test {};

TEST_F( pytorch_test , comparators ) {
  torch::Device device = torch::kCPU;
  std::cerr &lt;&lt; "MADE IT 1" &lt;&lt; std::endl;
  torch::Tensor tensor = torch::eye(2, device);
  std::cerr &lt;&lt; "MADE IT 2" &lt;&lt; std::endl;

  EXPECT_EQ(1, 1);
}
</code></pre>
<p>I obtain the output of both print statements, followed finally by:</p>
<pre><code class="lang-auto">*** Error in `/hdd2/code/build/exe/unitTest/debugNonOpt/unitTest': double free or corruption (!prev): 0x0000000002aa1290 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777f5)[0x7fffed4687f5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8038a)[0x7fffed47138a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fffed47558c]
/hdd2/dyna-mast/Adapt-HTAP/code/build/exe/unitTest/debugNonOpt/unitTest[0x62e351]
/usr/local/lib/libc10.so(+0x25624)[0x7ffff6f3b624]
/hdd2/code/build/exe/unitTest/debugNonOpt/unitTest[0x62e5ed]
/hdd2/code/build/exe/unitTest/debugNonOpt/unitTest[0x62d38a]
/hdd2/code/build/exe/unitTest/debugNonOpt/unitTest[0x62acf0]
/hdd2/code/build/exe/unitTest/debugNonOpt/unitTest[0x629478]
/usr/local/lib/libgmock.so(_ZN7testing8internal38HandleSehExceptionsInMethodIfSupportedINS_4TestEvEET0_PT_MS4_FS3_vEPKc+0x65)[0x7ffff794a1c1]
/usr/local/lib/libgmock.so(_ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS_4TestEvEET0_PT_MS4_FS3_vEPKc+0x5a)[0x7ffff7944179]
/usr/local/lib/libgmock.so(_ZN7testing4Test3RunEv+0xd2)[0x7ffff79276ea]
/usr/local/lib/libgmock.so(_ZN7testing8TestInfo3RunEv+0x10a)[0x7ffff792801e]
/usr/local/lib/libgmock.so(_ZN7testing8TestCase3RunEv+0x107)[0x7ffff79286b9]
/usr/local/lib/libgmock.so(_ZN7testing8internal12UnitTestImpl11RunAllTestsEv+0x2d2)[0x7ffff792f664]
/usr/local/lib/libgmock.so(_ZN7testing8internal38HandleSehExceptionsInMethodIfSupportedINS0_12UnitTestImplEbEET0_PT_MS4_FS3_vEPKc+0x65)[0x7ffff794b348]
/usr/local/lib/libgmock.so(_ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS0_12UnitTestImplEbEET0_PT_MS4_FS3_vEPKc+0x5a)[0x7ffff7944fad]
/usr/local/lib/libgmock.so(_ZN7testing8UnitTest3RunEv+0xc9)[0x7ffff792e1f1]
/hdd2/code/build/exe/unitTest/debugNonOpt/unitTest[0x657310]
/hdd2/code/build/exe/unitTest/debugNonOpt/unitTest[0x654d8e]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fffed411840]

</code></pre>
<p>Using GDB, I obtain the following backtrace:</p>
<pre><code class="lang-auto">*** Aborted at 1619133682 (unix time) try "date -d @1619133682" if you are using GNU date ***
PC: @                0x0 (unknown)
*** SIGABRT (@0x2cba000071f9) received by PID 29177 (TID 0x7f94e0d34100) from PID 29177; stack trace: ***
    @     0x7f94e01c4390 (unknown)
    @     0x7f94d617f438 gsignal
    @     0x7f94d618103a abort
    @     0x7f94d61c17fa (unknown)
    @     0x7f94d61ca38a (unknown)
    @     0x7f94d61ce58c cfree
    @           0x62e351 c10::VariableVersion::VersionCounter::~VersionCounter()
    @     0x7f94dfc94624 c10::TensorImpl::~TensorImpl()
    @           0x62e5ed c10::intrusive_ptr&lt;&gt;::reset_()
    @           0x62d38a c10::intrusive_ptr&lt;&gt;::~intrusive_ptr()
    @           0x62acf0 at::Tensor::~Tensor()
    @           0x629478 pytorch_test_comparators_Test::TestBody()
    @     0x7f94e06a31c1 testing::internal::HandleSehExceptionsInMethodIfSupported&lt;&gt;()
    @     0x7f94e069d179 testing::internal::HandleExceptionsInMethodIfSupported&lt;&gt;()
    @     0x7f94e06806ea testing::Test::Run()
    @     0x7f94e068101e testing::TestInfo::Run()
    @     0x7f94e06816b9 testing::TestCase::Run()
    @     0x7f94e0688664 testing::internal::UnitTestImpl::RunAllTests()
    @     0x7f94e06a4348 testing::internal::HandleSehExceptionsInMethodIfSupported&lt;&gt;()
    @     0x7f94e069dfad testing::internal::HandleExceptionsInMethodIfSupported&lt;&gt;()
    @     0x7f94e06871f1 testing::UnitTest::Run()
    @           0x657310 RUN_ALL_TESTS()
    @           0x654d8e main
    @     0x7f94d616a840 __libc_start_main
    @           0x4249b9 _start
    @                0x0 (unknown)
Aborted
</code></pre>
<p>Does anyone have any suggestions? Thank you!</p>