Heap error from cvtColor

Hi everyone!

Why does this simple code result in a heap error?

auto img = std::make_unique<wxImage>();
bool success = img->LoadFile(wxGetUserHome() + wxT("\\Desktop\\circleTest.jpg"));
cv::Mat src(img->GetHeight(), img->GetWidth(), CV_8UC3, img->GetData());
cv::Mat srcGray;
cvtColor(src, srcGray, cv::COLOR_BGR2GRAY);

using this image:

The error:

{707700} normal block at 0x0000022D8679D860, 2616 bytes long.
 Data: <                > F8 09 00 00 00 00 00 00 F4 09 00 00 00 00 00 00 
{706911} normal block at 0x0000022D867F94E0, 16 bytes long.
 Data: < :X             > C8 3A 58 F5 FD 7F 00 00 01 00 00 00 CD CD CD CD 
{706395} normal block at 0x0000022D867F7AA0, 16 bytes long.
 Data: < :X             > A0 3A 58 F5 FD 7F 00 00 00 00 00 00 CD CD CD CD 
{706392} normal block at 0x0000022D867F7A50, 8 bytes long.
 Data: <  N     > 18 0D 4E F5 FD 7F 00 00 

Thanx!

can you reproduce this without Wx being a dependency, and without a file being a dependency?

Thank you for answering!

I can confirm the the heap error is the same without wx dependency:
cv::Mat src = cv::imread("C:/circleTest.jpg");
cv::Mat srcGray;
cvtColor(src, srcGray, cv::COLOR_BGR2GRAY);

Unfortunatly, I do not know enough to be able to get rid of loading the image data from a file.

in place of imread, does a new blank matrix also serve to reproduce the issue?

a blank matrix does not reproduce the heap error.
how can I avoid it when loading a bitmap?

For the wxImage version, GetData() is RGB, so use cv::COLOR_RGB2GRAY for the intended grayscale conversion. That changes the color interpretation, but it doesn’t explain why the imread version also reports the error.

The Mat constructor wrapping GetData() doesn’t copy or own the pixels. Keep the wxImage and its data alive and unchanged for as long as the Mat uses them. Also check the row stride when wrapping any raw buffer: Mat’s default step assumes tightly packed rows (width × 3 bytes here). Pass the actual byte step if the source has padding. wxImage documents its GetData() as consecutive RGB triplets.

Since imread reproduces it too, could you share a minimal complete program and the exact diagnostic, including when it appears (during cvtColor or at shutdown)? The lines shown look like allocation records, so knowing what reports them would help distinguish a leak report from heap corruption.