RenderKit / ospray

An Open, Scalable, Portable, Ray Tracing Based Rendering Engine for High-Fidelity Visualization
http://ospray.org
Apache License 2.0
985 stars 180 forks source link

ospShutdown incorrectly reports errors #432

Closed paulmelis closed 2 years ago

paulmelis commented 4 years ago

While looking at #431 noticed that ospShutdown() gives errors when there's still alive ospray objects. E.g. running this

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <vector>
#include <chrono>
#include <thread>

#include <ospray/ospray_cpp.h>

using namespace ospcommon::math;

int main(int argc, const char **argv)
{
  // initialize OSPRay
  OSPError init_error = ospInit(&argc, argv);
  if (init_error != OSP_NO_ERROR)
    return init_error;

  // four vertex positions:
  std::vector<vec3f> vertex = {
      vec3f(-1.0f, -1.0f, 3.0f),
      vec3f(-1.0f, 1.0f, 3.0f),
      vec3f(1.0f, -1.0f, 3.0f),
      vec3f(0.1f, 0.1f, 0.3f)};

  // four vertex colors:
  std::vector<vec4f> color = {
      vec4f(0.9f, 0.5f, 0.5f, 1.0f),
      vec4f(0.8f, 0.8f, 0.8f, 1.0f),
      vec4f(0.8f, 0.8f, 0.8f, 1.0f),
      vec4f(0.5f, 0.9f, 0.5f, 1.0f)};

  // one triangle:
  vec3ui triangle(0, 1, 2);

  // Replicate the triangle N times. 
  // To notice the memory leak, we need the geometry to be more than just a handful of bytes.
  std::vector<vec3ui> index(10000);
  for(auto &t : index)
    t = triangle;

  //{

  // create and setup model and mesh
  ospray::cpp::Geometry mesh("mesh");
  mesh.setParam("vertex.position", ospray::cpp::Data(vertex));
  mesh.setParam("vertex.color", ospray::cpp::Data(color));
  mesh.setParam("index", ospray::cpp::Data(index));
  mesh.commit();

  // put the mesh into a model
  ospray::cpp::GeometricModel model(mesh);
  model.commit();

  //}

  ospShutdown();

  return 0;
}

gives

melis@juggle 11:51:~$ ./t_shutdown 
#ospray: INITIALIZATION ERROR --> OSPRay not yet initialized (most likely this means you tried to call an ospray API function before first calling ospInit())(pid 17134)
#ospray: INITIALIZATION ERROR --> OSPRay not yet initialized (most likely this means you tried to call an ospray API function before first calling ospInit())(pid 17134)

Apparently one error line per still-alive object. Removing the comments on the braces to put the two objects in local scope makes the errors on shutdown disappear. This is with 2.1.

johguenther commented 3 years ago

We fixed many similar issues, but this one still persists. The reason is that the cpp-wrappers manage the handles of created objects, and those are ospReleased in the destructor. In your example mesh and model are still alive after ospShutdown . Then both are destroyed at exit, resulting in two ospRelease at a time when there is no OSPRay device anymore, hence the two error messages. The local scoping on the other hand releases mesh and model before shutdown and all is good.

johguenther commented 2 years ago

I think the printed hints are still warranted (because there are API calls without a valid OSPRay device), but the message is a bit misleading.