GeomVertexWriter crashes

Hi everyone,
I am new to Panda3D and successfully worked my way through the hello world example.
As a second test I followed the manual to procedually generate a cube at runtime, but the program crashes.

Code:

#include <panda3d/pandaFramework.h>
#include <panda3d/pandaSystem.h>

#include <panda3d/geomTristrips.h>

GeomNode* makeCube()
{
    GeomVertexData* vdata = new GeomVertexData("cube",GeomVertexFormat::get_v3(),Geom::UH_static);
    vdata->set_num_rows(8);
    GeomVertexWriter vertices(vdata,"vertex");
    for(float z : {0.5f, -0.5f})
    {
        vertices.add_data3f(-0.5, -0.5, z);
        vertices.add_data3f(-0.5,  0.5, z);
        vertices.add_data3f(0.5, 0.5, z);
        vertices.add_data3f(0.5, -0.5, z);
    }
    GeomTristrips* strip = new GeomTristrips(Geom::UH_static);
    strip->add_vertices(3,2,6);
    strip->add_vertices(7,4,2,0);
    strip->add_vertices(3,1,6,5);
    strip->add_vertices(4,1,0);
    strip->close_primitive(); 
    Geom* geom = new Geom(vdata);
    geom->add_primitive(strip);
    GeomNode* node = new GeomNode("Mr. Cube");
    node->add_geom(geom);
    return node;
}

int main(int argc,char* argv[])
{
    PandaFramework framework;
    framework.open_framework(argc, argv);
    framework.set_window_title("My Panda3D Window");
    WindowFramework* window = framework.open_window();
    GeomNode* cube = makeCube();

    NodePath node = window->get_render().attach_new_node(cube);
    node.set_pos(0, 5, 0);

    framework.main_loop();
    framework.close_framework();
    return 0;
}

I have tested this with several versions of Panda3D:
-The precompiled binary (panda3d1.9_1.9.4~zesty_amd64.deb) aborts with
“built/include/mutexPosixImpl.I:129: void ReMutexPosixImpl::acquire(): Assertion `result == 0’ failed.”
upon creation of the GeomVertexWriter object (line 10)

-A self compiled installation simply segfaults at the same line (probably the same problem, but without the guarding assertion)

-A self compiled installation of Panda3D version 1.10 (from the git repository) segfaults at “strip->close_primitive()” (line 23)

I rewrote the code in python (basically a line-by-line copy) and it works without problems, so I guess I missed something that is implicitely done by the python bindings.

I am using Debian Stretch (testing) x64 and g++.

Hi, welcome to the forums! :slight_smile:

These are reference counted classes, so you should be using the PT() macro for managing reference counts properly. What’s probably happening is that your objects are being deleted because their reference counts are zero. This is the corrected code:

#include <panda3d/pandaFramework.h>
#include <panda3d/pandaSystem.h>

#include <panda3d/geomTristrips.h>

PT(GeomNode) makeCube()
{
    PT(GeomVertexData) vdata = new GeomVertexData("cube",GeomVertexFormat::get_v3(),Geom::UH_static);
    vdata->set_num_rows(8);
    GeomVertexWriter vertices(vdata,"vertex");
    for(float z : {0.5f, -0.5f})
    {
        vertices.add_data3f(-0.5, -0.5, z);
        vertices.add_data3f(-0.5,  0.5, z);
        vertices.add_data3f(0.5, 0.5, z);
        vertices.add_data3f(0.5, -0.5, z);
    }
    PT(GeomTristrips) strip = new GeomTristrips(Geom::UH_static);
    strip->add_vertices(3,2,6);
    strip->add_vertices(7,4,2,0);
    strip->add_vertices(3,1,6,5);
    strip->add_vertices(4,1,0);
    strip->close_primitive(); 
    PT(Geom) geom = new Geom(vdata);
    geom->add_primitive(strip);
    PT(GeomNode) node = new GeomNode("Mr. Cube");
    node->add_geom(geom);
    return node;
}

int main(int argc,char* argv[])
{
    PandaFramework framework;
    framework.open_framework(argc, argv);
    framework.set_window_title("My Panda3D Window");
    WindowFramework* window = framework.open_window();
    PT(GeomNode) cube = makeCube();

    NodePath node = window->get_render().attach_new_node(cube);
    node.set_pos(0, 5, 0);

    framework.main_loop();
    framework.close_framework();
    return 0;
}

Thanks, that was the problem :slight_smile:

I actually wondered (a) what happens to the memory allocated by new and (b) what the exact purpose of PT() is, but somehow it did not occur to me that one takes care of the other :laughing: