std::bad_alloc exception when building example

I am trying to build the cylinder example from the doc. I use cmake then open the project in Visual Studio. I am able to build the project but when I run it in debug mode I get the following error :

Exception thrown at 0x00007FFB241E4FD9 in main.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0000001D47EFF250.

It comes from the colors->SetColor("BkgColor",;. If I remove this line it also happens with other functions trying to set the color.

When I run the program in release mode, it runs fine.

What do you think is going wrong in debug mode ?

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <array>

int main(int, char*[])
  vtkNew<vtkNamedColors> colors;

  // Set the background color.
  std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};

  // This creates a polygonal cylinder model with eight circumferential facets
  // (i.e, in practice an octagonal prism).
  vtkNew<vtkCylinderSource> cylinder;

  // The mapper is responsible for pushing the geometry into the graphics
  // library. It may also do color mapping, if scalars or other attributes are
  // defined.
  vtkNew<vtkPolyDataMapper> cylinderMapper;

  // The actor is a grouping mechanism: besides the geometry (mapper), it
  // also has a property, transformation matrix, and/or texture map.
  // Here we set its color and rotate it around the X and Y axes.
  vtkNew<vtkActor> cylinderActor;

  // The renderer generates the image
  // which is then displayed on the render window.
  // It can be thought of as a scene to which the actor is added
  vtkNew<vtkRenderer> renderer;
  // Zoom in a little by accessing the camera and invoking its "Zoom" method.

  // The render window is the actual GUI window
  // that appears on the computer screen
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(300, 300);

  // The render window interactor captures mouse events
  // and will perform appropriate camera or actor manipulation
  // depending on the nature of the events.
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;

  // This starts the event loop and as a side effect causes an initial render.

  return EXIT_SUCCESS;

It might simply be the result of a dirty VTK build. Did you build the debug configuration of VTK yourself, with Visual Studio? Which versions of each? You could try doing a “Clean solution” and then rebuild the debug configuration.

The VTK development team has mostly moved away from Visual Studio to ninja for builds. Though ninja requires having completely separate build directories for Release vs Debug.

The issue will be incompatible DLLs. I would bet your VTK is built in release mode. So your build of the example should be in release mode not debug mode. Alternatively you can also build VTK in debug mode and set the path to the debug version of the VTK dlls before running the example.

Windows applications can be built with Debug , Release , RelWithDebInfo and MinSizeRel. Debug , Release DLLs are incompatible with each other. However you could build VTK with RelWithDebInfo and then the example should run OK if built with either Debug or Release.

It works fine with RelWithDebInfo. I will try to compile vtk in Debug to see if it works.

It should, as along as your windows path points to the Debug version of VTK.