Issue 1:
System information (version)

OpenCV => 4.2

Operating System / Platform => Ubuntu 16.04

Compiler => ❔

Detailed description

I'm using python bindings for opencv and found a bug with resizing images with 5 channels. It works fine for any other number of channels.

I've also checked previous versions and it works in 4.1.1. So the bug should be very recent

Steps to reproduce

import numpy as np
import cv2
arr = np.ones((1200, 1200, 5), dtype=np.uint8)
cv2.resize(arr, (512, 512))

for me it fails with

double free or corruption (!prev)

Issue 2:
System information (version)

OpenCV => 4 (June 2018, version from master)

Operating System / Platform => Windows 10; 64bit

Compiler => Visual Studio 2015: v140_xp

Detailed description

When I use the visual studio leak detector: https://archive.codeplex.com/?p=vld, I get tons of memory leaks for a very simple command line application using latest OpenCV, especially the dnn module.

Actually I get the same memory leaks without the vld-extension but same code within the constructor of a frame with wxWidgets 3.0. But this is not so simple to reproduce for others (you need wxWidgets), therefore I will post the code of a simple command line program.

Actually, I am not quiet sure, if the leak report is correct or if these are "false flags". Any help or comment is appreciated.

Steps to reproduce

#include "StdAfx.h"
#include <opencv2\core.hpp>
#include <opencv2\dnn.hpp>
#include <vld.h> //<-- for memory leak detection, see https://archive.codeplex.com/?p=vld
// entry point for MemoryLeakTestProjectOpenCV4
int main(int /*argc*/, char* /*argv*/[ ])
{
const int nWidth = 300;
const int nHeight = 300;
const std::string sPathToModel = "C:/Projects/SSS-FPCoding/MemoryLeakTestProjectOpenCV4/Debug MD/x64/test.pb";
cv::Mat oImage = cv::Mat::ones(cv::Size(nWidth, nHeight), CV_32FC3);
oImage;
cv::Mat oBlob = cv::dnn::blobFromImage(oImage - cv::Scalar(127.5, 127.5, 127.5), 1.0 / 127.5, cv::Size(nWidth, nHeight), cv::Scalar(0.0, 0.0, 0.0), true, false);
cv::dnn::Net oNet;
oNet = cv::dnn::readNetFromTensorflow(sPathToModel);
oNet.setInput(oBlob);
cv::Mat oDetectionMat = oNet.forward();
return 0;
}

When running this simple program, I get the following leak detection report:

WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00000000C7FCA570: 80 bytes ----------
Leak Hash: 0xF3029A69, Count: 1, Total 80 bytes
Call Stack (TID 10700):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): MemoryLeakTestCmdAndOpenCV4.exe!operator new() + 0xA bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\system.cpp (58): MemoryLeakTestCmdAndOpenCV4.exe!cv::getInitializationMutex() + 0xA bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\system.cpp (62): MemoryLeakTestCmdAndOpenCV4.exe!cv::`dynamic initializer for '__initialization_mutex_initializer''() + 0x1A bytes
ucrtbased.dll!initterm() + 0x5D bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (223): MemoryLeakTestCmdAndOpenCV4.exe!__scrt_common_main_seh()
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (296): MemoryLeakTestCmdAndOpenCV4.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): MemoryLeakTestCmdAndOpenCV4.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
ntdll.dll!RtlUserThreadStart() + 0x21 bytes
Data:
02 01 00 00 CD CD CD CD B8 25 97 90 F8 7F 00 00 ........ .%......
00 00 00 00 00 00 00 00 CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD FF FF FF FF 00 00 00 00 ........ ........

---------- Block 38732 at 0x00000000C7FCEA20: 256 bytes ----------
Leak Hash: 0xC671A8C8, Count: 1, Total 256 bytes
Call Stack (TID 10700):
ucrtbased.dll!malloc()
c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0 (977): MemoryLeakTestCmdAndOpenCV4.exe!std::_Wrap_alloc<std::allocator<void * __ptr64> >::allocate()
c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector (1602): MemoryLeakTestCmdAndOpenCV4.exe!std::vector<void * __ptr64,std::allocator<void * __ptr64> >::_Reallocate() + 0x1D bytes
c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector (1039): MemoryLeakTestCmdAndOpenCV4.exe!std::vector<void * __ptr64,std::allocator<void * __ptr64> >::reserve()
c:\users\sebert\downloads\opencv-master\modules\core\src\system.cpp (1301): MemoryLeakTestCmdAndOpenCV4.exe!cv::ThreadData::ThreadData() + 0x12 bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\system.cpp (1441): MemoryLeakTestCmdAndOpenCV4.exe!cv::TlsStorage::setData() + 0x2A bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\system.cpp (1518): MemoryLeakTestCmdAndOpenCV4.exe!cv::TLSDataContainer::getData()
c:\users\sebert\downloads\opencv-master\modules\core\include\opencv2\core\utility.hpp (684): MemoryLeakTestCmdAndOpenCV4.exe!cv::TLSData<cv::CoreTLSData>::get() + 0x28 bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\ocl.cpp (880): MemoryLeakTestCmdAndOpenCV4.exe!cv::ocl::useOpenCL() + 0xD bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\umatrix.cpp (232): MemoryLeakTestCmdAndOpenCV4.exe!cv::UMat::getStdAllocator() + 0x5 bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\umatrix.cpp (372): MemoryLeakTestCmdAndOpenCV4.exe!cv::Mat::getUMat() + 0x5 bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\layers\convolution_layer.cpp (1596): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::ConvolutionLayer::create() + 0x2F bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\include\opencv2\dnn\layer.details.hpp (53): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::details::_layerDynamicRegisterer<cv::dnn::experimental_dnn_v4::ConvolutionLayer>() + 0xF bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (2970): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::LayerFactory::createLayerInstance() + 0x2B bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (389): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::LayerData::getLayerInstance() + 0x28 bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (1902): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::Impl::getLayerShapesRecursively() + 0x2C bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (1915): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::Impl::getLayersShapes() + 0x21 bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (1749): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::Impl::allocateLayers()
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (875): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::Impl::setUpNet()
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (2080): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::forward() + 0x45 bytes
c:\projects\sss-fpcoding\memoryleaktestprojectopencv4\memoryleaktestprojectopencv4.cpp (17): MemoryLeakTestCmdAndOpenCV4.exe!main() + 0x42 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (65): MemoryLeakTestCmdAndOpenCV4.exe!invoke_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (253): MemoryLeakTestCmdAndOpenCV4.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (296): MemoryLeakTestCmdAndOpenCV4.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): MemoryLeakTestCmdAndOpenCV4.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
ntdll.dll!RtlUserThreadStart() + 0x21 bytes
Data:
F0 8E 95 C8 D5 01 00 00 B0 38 FE C7 D5 01 00 00 ........ .8......
D0 61 A5 D0 D5 01 00 00 CD CD CD CD CD CD CD CD .a...... ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........

---------- Block 38746 at 0x00000000C7FCF060: 256 bytes ----------
Leak Hash: 0x03BE8C52, Count: 1, Total 256 bytes
Call Stack (TID 10700):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): MemoryLeakTestCmdAndOpenCV4.exe!operator new() + 0xA bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\ocl.cpp (2371): MemoryLeakTestCmdAndOpenCV4.exe!cv::ocl::Context::getDefault() + 0xA bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\ocl.cpp (1617): MemoryLeakTestCmdAndOpenCV4.exe!cv::ocl::Device::getDefault() + 0x7 bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\ocl.cpp (885): MemoryLeakTestCmdAndOpenCV4.exe!cv::ocl::useOpenCL() + 0x11 bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\umatrix.cpp (232): MemoryLeakTestCmdAndOpenCV4.exe!cv::UMat::getStdAllocator() + 0x5 bytes
c:\users\sebert\downloads\opencv-master\modules\core\src\umatrix.cpp (372): MemoryLeakTestCmdAndOpenCV4.exe!cv::Mat::getUMat() + 0x5 bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\layers\convolution_layer.cpp (1596): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::ConvolutionLayer::create() + 0x2F bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\include\opencv2\dnn\layer.details.hpp (53): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::details::_layerDynamicRegisterer<cv::dnn::experimental_dnn_v4::ConvolutionLayer>() + 0xF bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (2970): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::LayerFactory::createLayerInstance() + 0x2B bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (389): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::LayerData::getLayerInstance() + 0x28 bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (1902): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::Impl::getLayerShapesRecursively() + 0x2C bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (1915): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::Impl::getLayersShapes() + 0x21 bytes
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (1749): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::Impl::allocateLayers()
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (875): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::Impl::setUpNet()
c:\users\sebert\downloads\opencv-master\modules\dnn\src\dnn.cpp (2080): MemoryLeakTestCmdAndOpenCV4.exe!cv::dnn::experimental_dnn_v4::Net::forward() + 0x45 bytes
c:\projects\sss-fpcoding\memoryleaktestprojectopencv4\memoryleaktestprojectopencv4.cpp (17): MemoryLeakTestCmdAndOpenCV4.exe!main() + 0x42 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (65): MemoryLeakTestCmdAndOpenCV4.exe!invoke_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (253): MemoryLeakTestCmdAndOpenCV4.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (296): MemoryLeakTestCmdAndOpenCV4.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): MemoryLeakTestCmdAndOpenCV4.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
ntdll.dll!RtlUserThreadStart() + 0x21 bytes
Data:
01 00 00 00 CD CD CD CD 20 29 FE C7 D5 01 00 00 ........ .)......
F0 AC 93 C8 D5 01 00 00 B0 E0 03 C8 D5 01 00 00 ........ ........
B8 E0 03 C8 D5 01 00 00 B8 E0 03 C8 D5 01 00 00 ........ ........
80 A9 93 C8 D5 01 00 00 00 CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD 00 00 00 00 00 00 00 00 ........ ........
0F 00 00 00 00 00 00 00 70 AA 93 C8 D5 01 00 00 ........ p.......
00 CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
00 00 00 00 00 00 00 00 0F 00 00 00 00 00 00 00 ........ ........
02 01 00 00 CD CD CD CD B8 25 97 90 F8 7F 00 00 ........ .%......
00 00 00 00 00 00 00 00 CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD FF FF FF FF 00 00 00 00 ........ ........
C0 A5 93 C8 D5 01 00 00 C0 29 00 C8 D5 01 00 00 ........ .)......
00 00 00 00 00 00 00 00 C0 AA 93 C8 D5 01 00 00 ........ ........
10 D8 0B C8 D5 01 00 00 00 00 00 00 00 00 00 00 ........ ........

...many more blocks...
...please see attached .txt for full leak report...
[memoryleakreport_30.7.2018.opencv4.txt](https://github.com/opencv/opencv/files/2240396/memoryleakreport_30.7.2018.opencv4.txt)

Visual Leak Detector detected 128 memory leaks (14924 bytes).
Largest number used: 20520922 bytes.
Total allocations: 46858788 bytes.
Visual Leak Detector is now exiting.
The program '[9328] MemoryLeakTestCmdAndOpenCV4.exe' has exited with code 0 (0x0).