Creating new MouseWatcher crashes

Minimal code to reproduce:

#include <mouseWatcher.h>

int main()
{
	MouseWatcher* mw = new MouseWatcher("mw");

	return 0;
}

The program crashes:

Exception thrown at 0x00007FFBC1E7D891 (ntdll.dll) in game.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

What am I doing wrong?

Turned out it crashed only building against my custom Panda3d build (only core libraries included).
Weird.

No, I was wrong.

The newest 1.10.6 version from the downloads section doesn’t work, too.
It just throws “heap corrupted” errors in different places if I add this line:

MouseWatcher* mw = new MouseWatcher("mw");

This is driving me crazy. I don’t understand what is wrong.

Two things:
Firstly, you need to store it as a PT(MouseWatcher), because it is a reference-counted class. If you don’t hold a reference to it, the reference count will be zero and Panda3D may delete it at will.

PT(MouseWatcher) mw = new MouseWatcher("mw");

Secondly, you need to make sure that you compile in Release mode, because the Panda3D downloads are also compiled in Release mode.

Thank you for your reply, rdb. Unfortunately, wrapping a pointer in a PT makes little difference.
I can run the code in the first post (create a MouseWatcher and exit), but in a more useful situation I still get heap corruption. E.g.:

#include <mouseWatcher.h>
#include <pandaFramework.h>

int main()
{
	PandaFramework pf = PandaFramework();
	pf.open_framework();
	WindowFramework* wf = pf.open_window();

	PT(MouseWatcher) mw = new MouseWatcher("mw");

	pf.main_loop();

	nout << "done\n";

	return 0;
}

The last line in the debug log:

:pipeline(debug): Beginning the pipeline cycle

It still crashes with c0000374: heap corruption error.

Yes, I am building in release mode. Without MouseWatcher it runs normally.

Thanks to rdb, problem solved.

It turned out that cmake+ninja build system is impicitly inserting /D NDEBUG parameter in the compiler command line argumetns.

The manual says not to do so:
https://docs.panda3d.org/1.10/cpp/introduction/running-your-program?highlight=ndebug