Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can i debug a C++ delete call on android NDK?
    text
    copied!<p>I've a Android C++ application that is manged by the Java layer. In this code i use a old physics library (tokamak) and i do almost nothing, i create and delete the simulator like this:</p> <pre><code>static neSimulator *gSim; neV3 gravity; gravity.Set(0.0f, -10.f, 0.0f); neSimulatorSizeInfo sizeInfo; sizeInfo.rigidBodiesCount = 1; sizeInfo.animatedBodiesCount = 1; sizeInfo.geometriesCount = 2; sizeInfo.overlappedPairsCount = 2; gSim = neSimulator::CreateSimulator(sizeInfo, NULL, &amp;gravity); </code></pre> <p>And the destroy it:</p> <pre><code>neSimulator::CreateSimulator(gSim); </code></pre> <p>This works, the problem appears when i start adding geometry:</p> <pre><code>neV3 ballPos; rgdBall = gSim-&gt;CreateRigidBody(); neGeometry *geoBall = rgdBall-&gt;AddGeometry(); geoBall-&gt;SetSphereDiameter(1.5f); rgdBall-&gt;UpdateBoundingInfo(); rgdBall-&gt;SetMass(2.0f); rgdBall-&gt;SetInertiaTensor(neSphereInertiaTensor(1.5f, 2.0f)); ballPos.Set(0.0f, 5.0f, 0.0f); rgdBall-&gt;SetPos(ballPos); </code></pre> <p>In this case when i call the destroy (and i only call it once) i get a SIGSEGV (Null Pointer) deadbaad.</p> <p>I've all debugging log statements to the destructor method and the code inside the destructor completes to the end. So there is this code:</p> <pre><code>void neSimulator::DestroySimulator(neSimulator * sim) { __android_log_print(ANDROID_LOG_INFO, "TOKAMAK", "Before cast"); neFixedTimeStepSimulator * s = reinterpret_cast&lt;neFixedTimeStepSimulator *&gt;(sim); __android_log_print(ANDROID_LOG_INFO, "TOKAMAK", "After cast"); __android_log_print(ANDROID_LOG_INFO, "TOKAMAK", "Before delete"); delete s; __android_log_print(ANDROID_LOG_INFO, "TOKAMAK", "After delete"); } </code></pre> <p>So i log the destructor:</p> <pre><code>neFixedTimeStepSimulator::~neFixedTimeStepSimulator() { FreeAllBodies(); if (perf) delete perf; __android_log_print(ANDROID_LOG_INFO, "TOKAMAK", "dtor complete"); } </code></pre> <p>What is messing me is that i see the dtor complete message on the log but not the After delete message and a SIGSEGV error.</p> <p>How can i investigate it better?</p> <p>[More info after further investigation]</p> <p>So i used the addr2line tool to investigate the stack trace and traced the error to the default memory allocator. So i added logging to all alloc and free calls:</p> <pre><code>03-23 13:31:14.617: INFO/neAllocatorDefault(326): malloc 0x1b3fd8 size 2292 03-23 13:31:14.617: INFO/neAllocatorDefault(326): malloc 0x1b48d0 size 488 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x44ae3008 size 114404 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x1a58b8 size 8 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x1b4ac0 size 800 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x1b4de8 size 416 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x1b4f90 size 836 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x1aca10 size 44 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x1b52d8 size 2500 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x1b5ca0 size 2500 03-23 13:31:14.627: INFO/neAllocatorDefault(326): malloc 0x1b6668 size 2500 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1b7030 size 400 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1b71c8 size 800 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x424ed008 size 72404 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1b74f0 size 4004 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1b8498 size 2044 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1b8c98 size 6044 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1ba438 size 5004 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1bb7c8 size 11204 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1be390 size 340 03-23 13:31:14.637: INFO/neAllocatorDefault(326): malloc 0x1be4e8 size 4000 03-23 13:31:14.647: INFO/neAllocatorDefault(326): malloc 0x1bf490 size 4000 03-23 13:31:14.647: INFO/neAllocatorDefault(326): malloc 0x1c0438 size 38800 03-23 13:31:14.647: INFO/neAllocatorDefault(326): malloc 0x1c9bd0 size 38800 </code></pre> <p>And</p> <pre><code>03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b71c8 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b7030 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b6668 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b5ca0 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b52d8 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1aca10 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b4f90 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b4de8 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b4ac0 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1a58b8 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x44ae3008 03-23 13:31:19.508: INFO/neAllocatorDefault(326): free 0x1b48d0 </code></pre> <p>So the SIGSEGV happens when trying to free 0x1b48d0, the funny thing is that there is a previous malloc that returned that pointer and no previous free. I am even more puzzled now...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload