Skip to content

Commit 34f9c03

Browse files
committed
Merge pull request opencv#9238 from alalek:valgrind_fixes
2 parents 06407b4 + 12213f9 commit 34f9c03

File tree

12 files changed

+445
-145
lines changed

12 files changed

+445
-145
lines changed

cmake/OpenCVCompilerOptions.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ if(CMAKE_COMPILER_IS_GNUCXX)
203203
endif()
204204

205205
set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
206-
set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0 -DDEBUG -D_DEBUG")
206+
if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} " MATCHES "-O")
207+
set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0")
208+
endif()
209+
set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
207210
endif()
208211

209212
if(MSVC)

cmake/OpenCVUtils.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ macro(ocv_finalize_status)
533533
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${OPENCV_BUILD_INFO_FILE}" "${OPENCV_MODULE_opencv_core_BINARY_DIR}/version_string.inc" OUTPUT_QUIET)
534534
endif()
535535
endif()
536+
537+
if(UNIX)
538+
install(FILES "${OpenCV_SOURCE_DIR}/platforms/scripts/valgrind.supp"
539+
"${OpenCV_SOURCE_DIR}/platforms/scripts/valgrind_3rdparty.supp"
540+
DESTINATION "${OPENCV_OTHER_INSTALL_PATH}" COMPONENT "dev")
541+
endif()
536542
endmacro()
537543

538544

modules/calib3d/src/stereobm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ prefilterXSobel( const Mat& src, Mat& dst, int ftzero )
197197
{
198198
int x, y;
199199
const int OFS = 256*4, TABSZ = OFS*2 + 256;
200-
uchar tab[TABSZ];
200+
uchar tab[TABSZ] = { 0 };
201201
Size size = src.size();
202202

203203
for( x = 0; x < TABSZ; x++ )
@@ -227,7 +227,7 @@ prefilterXSobel( const Mat& src, Mat& dst, int ftzero )
227227
v_int16x8 ftz2 = v_setall_s16((short)(ftzero*2));
228228
v_int16x8 z = v_setzero_s16();
229229

230-
for(; x <= size.width-8; x += 8 )
230+
for(; x <= (size.width - 1) - 8; x += 8 )
231231
{
232232
v_int16x8 s00 = v_reinterpret_as_s16(v_load_expand(srow0 + x + 1));
233233
v_int16x8 s01 = v_reinterpret_as_s16(v_load_expand(srow0 + x - 1));

modules/core/src/stat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ static bool ocl_meanStdDev( InputArray _src, OutputArray _mean, OutputArray _sdv
15471547

15481548
bool haveMask = _mask.kind() != _InputArray::NONE;
15491549
int nz = haveMask ? -1 : (int)_src.total();
1550-
Scalar mean, stddev;
1550+
Scalar mean(0), stddev(0);
15511551
const int cn = _src.channels();
15521552
if (cn > 4)
15531553
return false;

modules/dnn/src/init.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include "precomp.hpp"
4343
#include <opencv2/dnn/layer.details.hpp>
4444

45+
#include <google/protobuf/stubs/common.h>
46+
4547
namespace cv {
4648
namespace dnn {
4749
CV__DNN_EXPERIMENTAL_NS_BEGIN
@@ -56,11 +58,26 @@ Mutex& getInitializationMutex()
5658
// force initialization (single-threaded environment)
5759
Mutex* __initialization_mutex_initializer = &getInitializationMutex();
5860

61+
namespace {
62+
using namespace google::protobuf;
63+
class ProtobufShutdown {
64+
public:
65+
bool initialized;
66+
ProtobufShutdown() : initialized(true) {}
67+
~ProtobufShutdown()
68+
{
69+
initialized = false;
70+
google::protobuf::ShutdownProtobufLibrary();
71+
}
72+
};
73+
} // namespace
5974

6075
void initializeLayerFactory()
6176
{
6277
CV_TRACE_FUNCTION();
6378

79+
static ProtobufShutdown protobufShutdown; (void)protobufShutdown;
80+
6481
CV_DNN_REGISTER_LAYER_CLASS(Slice, SliceLayer);
6582
CV_DNN_REGISTER_LAYER_CLASS(Split, SplitLayer);
6683
CV_DNN_REGISTER_LAYER_CLASS(Concat, ConcatLayer);

modules/flann/include/opencv2/flann/lsh_table.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class LshTable
147147
LshTable()
148148
{
149149
key_size_ = 0;
150+
feature_size_ = 0;
150151
speed_level_ = kArray;
151152
}
152153

@@ -157,7 +158,7 @@ class LshTable
157158
*/
158159
LshTable(unsigned int feature_size, unsigned int key_size)
159160
{
160-
(void)feature_size;
161+
feature_size_ = feature_size;
161162
(void)key_size;
162163
std::cerr << "LSH is not implemented for that type" << std::endl;
163164
assert(0);
@@ -332,6 +333,8 @@ class LshTable
332333
*/
333334
unsigned int key_size_;
334335

336+
unsigned int feature_size_;
337+
335338
// Members only used for the unsigned char specialization
336339
/** The mask to apply to a feature to get the hash key
337340
* Only used in the unsigned char case
@@ -345,9 +348,10 @@ class LshTable
345348
template<>
346349
inline LshTable<unsigned char>::LshTable(unsigned int feature_size, unsigned int subsignature_size)
347350
{
351+
feature_size_ = feature_size;
348352
initialize(subsignature_size);
349353
// Allocate the mask
350-
mask_ = std::vector<size_t>((size_t)ceil((float)(feature_size * sizeof(char)) / (float)sizeof(size_t)), 0);
354+
mask_ = std::vector<size_t>((feature_size * sizeof(char) + sizeof(size_t) - 1) / sizeof(size_t), 0);
351355

352356
// A bit brutal but fast to code
353357
std::vector<size_t> indices(feature_size * CHAR_BIT);
@@ -392,6 +396,7 @@ inline size_t LshTable<unsigned char>::getKey(const unsigned char* feature) cons
392396
{
393397
// no need to check if T is dividable by sizeof(size_t) like in the Hamming
394398
// distance computation as we have a mask
399+
// FIXIT: This is bad assumption, because we reading tail bytes after of the allocated features buffer
395400
const size_t* feature_block_ptr = reinterpret_cast<const size_t*> ((const void*)feature);
396401

397402
// Figure out the subsignature of the feature
@@ -400,10 +405,20 @@ inline size_t LshTable<unsigned char>::getKey(const unsigned char* feature) cons
400405
size_t subsignature = 0;
401406
size_t bit_index = 1;
402407

403-
for (std::vector<size_t>::const_iterator pmask_block = mask_.begin(); pmask_block != mask_.end(); ++pmask_block) {
408+
for (unsigned i = 0; i < feature_size_; i += sizeof(size_t)) {
404409
// get the mask and signature blocks
405-
size_t feature_block = *feature_block_ptr;
406-
size_t mask_block = *pmask_block;
410+
size_t feature_block;
411+
if (i <= feature_size_ - sizeof(size_t))
412+
{
413+
feature_block = *feature_block_ptr;
414+
}
415+
else
416+
{
417+
size_t tmp = 0;
418+
memcpy(&tmp, feature_block_ptr, feature_size_ - i); // preserve bytes order
419+
feature_block = tmp;
420+
}
421+
size_t mask_block = mask_[i / sizeof(size_t)];
407422
while (mask_block) {
408423
// Get the lowest set bit in the mask block
409424
size_t lowest_bit = mask_block & (-(ptrdiff_t)mask_block);

0 commit comments

Comments
 (0)