Error when accessing at::Tensor using LibTorch in C++ - c++

I have this very simple code snippet using opencv and LibTorch, which does not run for some reason.
#include <iostream>
#include <torch/script.h>
#include <opencv2/core/core.hpp>
int main() {
cv::Mat imgMat = cv::Mat::zeros(640, 640, CV_8UC3);
at::Tensor tensorImg = torch::from_blob(imgMat.data, {1, imgMat.rows, imgMat.cols, imgMat.channels()});
std::cout << tensorImg << "\n"; // problem here
return 0;
}
I have tried to compile it with clang and added undefined behaviour sanitizer, which gives the following errors:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==11549==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7fffde2fa000 (pc 0x7fffe4039d08 bp 0x7fffdd7b4ed0 sp 0x7fffdd7b4e20 T11570)
==11549==The signal is caused by a READ memory access.
UndefinedBehaviorSanitizer:DEADLYSIGNAL
UndefinedBehaviorSanitizer:DEADLYSIGNAL
#0 0x7fffe4039d08 in void c10::function_ref<void (char**, long const*, long, long)>::callback_fn<auto at::TensorIteratorBase::loop_2d_from_1d<at::native::AVX2::copy_kernel(at::TensorIterator&, bool)::'lambda'()::operator()() const::'lambda10'()::operator()() const::'lambda'()::operator()() const::'lambda12'()::operator()() const::'lambda'(char**, long const*, long)>(at::native::AVX2::copy_kernel(at::TensorIterator&, bool)::'lambda'()::operator()() const::'lambda10'()::operator()() const::'lambda'()::operator()() const::'lambda12'()::operator()() const::'lambda'(char**, long const*, long) const&)::'lambda'(char**, long const*, long, long)>(long, char**, long const*, long, long) (/home/dani/Desktop/test/build/libtorch/lib/libtorch_cpu.so+0x54bed08) (BuildId: e03155c98263c3ef83236051d8610270872897af)
#1 0x7fffdfecf96f in at::TensorIteratorBase::serial_for_each(c10::function_ref<void (char**, long const*, long, long)>, at::Range) const (/home/dani/Desktop/test/build/libtorch/lib/libtorch_cpu.so+0x135496f) (BuildId: e03155c98263c3ef83236051d8610270872897af)
#2 0x7fffdfecfb2d in void at::internal::invoke_parallel<at::TensorIteratorBase::for_each(c10::function_ref<void (char**, long const*, long, long)>, long)::'lambda'(long, long)>(long, long, long, at::TensorIteratorBase::for_each(c10::function_ref<void (char**, long const*, long, long)>, long)::'lambda'(long, long) const&) (._omp_fn.0) (/home/dani/Desktop/test/build/libtorch/lib/libtorch_cpu.so+0x1354b2d) (BuildId: e03155c98263c3ef83236051d8610270872897af)
#3 0x7fffde41696d (/home/dani/Desktop/test/build/libtorch/lib/libgomp-52f2fd74.so.1+0x1696d) (BuildId: 9afb2d23e5127e68ba5ef6031eefc9d25b9b672b)
#4 0x7fffde79db42 in start_thread nptl/./nptl/pthread_create.c:442:8
#5 0x7fffde82f9ff misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV (/home/dani/Desktop/test/build/libtorch/lib/libtorch_cpu.so+0x54bed08) (BuildId: e03155c98263c3ef83236051d8610270872897af) in void c10::function_ref<void (char**, long const*, long, long)>::callback_fn<auto at::TensorIteratorBase::loop_2d_from_1d<at::native::AVX2::copy_kernel(at::TensorIterator&, bool)::'lambda'()::operator()() const::'lambda10'()::operator()() const::'lambda'()::operator()() const::'lambda12'()::operator()() const::'lambda'(char**, long const*, long)>(at::native::AVX2::copy_kernel(at::TensorIterator&, bool)::'lambda'()::operator()() const::'lambda10'()::operator()() const::'lambda'()::operator()() const::'lambda12'()::operator()() const::'lambda'(char**, long const*, long) const&)::'lambda'(char**, long const*, long, long)>(long, char**, long const*, long, long)
==11549==ABORTING
Any idea what am I doing wrong?

As it turned out I was missing the option parameter at::kByte from the function torch::from_blob().
Edit:
Without this parameter LibTorch could not interpret the tensor, and gave a deadly signal. See the actual reason in #Dan Mašek's comment.
Based on the documentation
The TensorOptions specify additional configuration options for the
returned tensor, such as what type to interpret the data as.
The correct line is:
at::Tensor tensorImg = torch::from_blob(imgMat.data, {1, imgMat.rows, imgMat.cols, imgMat.channels()}, at::kByte);

Related

clang++ adress sanitizer shows memory leaks even when there is no leak

I build with brew version of clang++ and use adress sanitizer to look for memory leaks and it gives memory leak on every program even programs without any leak.
clang++ -fsanitize=thread main.cpp -g
int main() {
auto *p = new int;
delete p; // no leak
return 0;
}
I have been using the following commands. I expect there shouldn’t be any leaks however it shows the leaks from system libraries couldn't make any sense.
clang++ -fsanitize=address main.cpp -g
export ASAN_OPTIONS=detect_leaks=1
export MallocNanoZone=0
./a.out
=================================================================
==74341==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 1952 byte(s) in 61 object(s) allocated from:
#0 0x1066d25e5 in wrap_calloc+0xa5 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x475e5) (BuildId: e487ca41363b3ac1b8e9e49fecb969fb2400000010000000000a0a0000000d00)
#1 0x7ff81bb972ee in realizeClassWithoutSwift(objc_class*, objc_class*)+0x85 (libobjc.A.dylib:x86_64h+0x52ee) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00)
#2 0x7ff81bb95646 in map_images_nolock+0x160e (libobjc.A.dylib:x86_64h+0x3646) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00)
#3 0x7ff81bb93fda in map_images+0x42 (libobjc.A.dylib:x86_64h+0x1fda) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00)
#4 0x7ff81bbe04c2 in invocation function for block in dyld4::RuntimeState::setObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*), void (*)(mach_header const*, void*, mach_header const*, void const*), void (*)(unsigned int, _dyld_objc_notify_mapped_info const*))+0x27c (dyld:x86_64+0xfffffffffff7e4c2) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
#5 0x7ff81bbdaffe in dyld4::RuntimeState::withLoadersReadLock(void () block_pointer)+0x2e
Direct leak of 1952 byte(s) in 61 object(s) allocated from:
#0 0x1066d25e5 in wrap_calloc+0xa5 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x475e5) (BuildId: e487ca41363b3ac1b8e9e49fecb969fb2400000010000000000a0a0000000d00)
#1 0x7ff81bb972ee in realizeClassWithoutSwift(objc_class*, objc_class*)+0x85 (libobjc.A.dylib:x86_64h+0x52ee) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00)
#2 0x7ff81bb973ac in realizeClassWithoutSwift(objc_class*, objc_class*)+0x143 (libobjc.A.dylib:x86_64h+0x53ac) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00)
#3 0x7ff81bb95646 in map_images_nolock+0x160e (libobjc.A.dylib:x86_64h+0x3646) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00)
#4 0x7ff81bb93fda in map_images+0x42 (libobjc.A.dylib:x86_64h+0x1fda) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00)
#5 0x7ff81bbe04c2 in invocation function for block in dyld4::RuntimeState::setObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*), void (*)(mach_header const*, void*, mach_header const*, void const*), void (*)(unsigned int, _dyld_objc_notify_mapped_info const*))+0x27c (dyld:x86_64+0xfffffffffff7e4c2) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
#6 0x7ff81bbdaffe in dyld4::RuntimeState::withLoadersReadLock(void () block_pointer)+0x2e (dyld:x86_64+0xfffffffffff78ffe) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
#7 0x7ff81bbe023f in dyld4::RuntimeState::setObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*), void (*)(mach_header const*, void*, mach_header const*, void const*), void (*)(unsigned int, _dyld_objc_notify_mapped_info const*))+0x5f (dyld:x86_64+0xfffffffffff7e23f) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
#8 0x7ff81bc045e3 in dyld4::APIs::_dyld_objc_register_callbacks(_dyld_objc_callbacks const*)+0x89 (dyld:x86_64+0xfffffffffffa25e3) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
#9 0x7ff81bb93e3e in _objc_init+0x4f6 (libobjc.A.dylib:x86_64h+0x1e3e) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00)
#10 0x7ff81bd850bf in _os_object_init+0xc (libdispatch.dylib:x86_64+0x20bf) (BuildId: 817339a1d03e3e549c47acacf69f619332000000200000000100000000000d00)
#11 0x7ff81bd92d34 in libdispatch_init+0x16a (libdispatch.dylib:x86_64+0xfd34) (BuildId: 817339a1d03e3e549c47acacf69f619332000000200000000100000000000d00)
#12 0x7ff827b2d894 in libSystem_initializer+0xed (libSystem.B.dylib:x86_64+0x1894) (BuildId: 862b6758852e3e89a4fed564a7163e2532000000200000000100000000000d00)
#13 0x7ff81bbea617 in invocation function for block in dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const+0xab (dyld:x86_64+0xfffffffffff88617) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
#14 0x7ff81bc29de8 in invocation function for block in dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const+0xf1 (dyld:x86_64+0xfffffffffffc7de8) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
#15 0x7ff81bc1def6 in invocation function for block in dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const+0x22c (dyld:x86_64+0xfffffffffffbbef6) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
...
#23 0x7ff81bbd5368 in dyld4::prepare(dyld4::APIs&, dyld3::MachOAnalyzer const*)+0xe9e (dyld:x86_64+0xfffffffffff73368) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
#24 0x7ff81bbd4280 in start+0x8f0 (dyld:x86_64+0xfffffffffff72280) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00)
SUMMARY: AddressSanitizer: 4288 byte(s) leaked in 134 allocation(s).
and my clang++ version:
Homebrew clang version 15.0.6
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
on macOS 13.0.1
Same situation here as the OP. I don't know what the root cause is (false positive vs. true memory leak in realizeClassWithoutSwift()), but you can configure LeakSanitizer to suppress detected leaks in the memory leak report:
Create a file lsan.supp with
leak:realizeClassWithoutSwift
and then use it when running your binary:
ASAN_OPTIONS=detect_leaks=1 LSAN_OPTIONS=suppressions=lsan.supp my_binary
More details at https://clang.llvm.org/docs/AddressSanitizer.html#suppressing-memory-leaks.
Answer courtesy to GitHub user's willmcpherson2 comment at google/sanitizers/issues/1501.

gRPC C++: AddressSanitizer: bad-free

gRPC v1.30.0
I created a grpc service and tried to run it. The execution goes smooth till the last return statement at server side.
Status theService(ServerContext *context, const Request* req, Response* res)
{
Status status = actualLogic(req,res);
//execution goes fine till here
return status;
}
Status actualLogic(req,res)
{
Response_NestedMsg msg;
msg.set_something(...);
res->mutable_nestedmsg()->CopyFrom(msg);
return Status::OK
}
//server startup code
ServerBuilder builder;
builder.AddListeningPort((address),grpc::InsecureServerCredentials());
builder.RegisterService(&serviceClassObj);
std::unique_ptr<Server> server(builder.BuildAndStart());
server->Wait();
Running this code, I get following runtime error
==14394==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x61b00000fcc8 in thread T5 (grpcpp_sync_ser)
#0 0x7fe9d35602c0 in operator delete(void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe12c0)
#1 0x55cb87299afd in __gnu_cxx::new_allocator<std::_List_node<grpc_impl::Server const*> >::deallocate(std::_List_node<grpc_impl::Server const*>*, unsigned long) (/home/john/Desktop/my_executable+0xd0afd)
#2 0x55cb87297ba1 in std::allocator_traits<std::allocator<std::_List_node<grpc_impl::Server const*> > >::deallocate(std::allocator<std::_List_node<grpc_impl::Server const*> >&, std::_List_node<grpc_impl::Server const*>*, unsigned long) (/home/john/Desktop/my_executable+0xceba1)
#3 0x55cb8729448d in std::__cxx11::_List_base<grpc_impl::Server const*, std::allocator<grpc_impl::Server const*> >::_M_put_node(std::_List_node<grpc_impl::Server const*>*) (/home/john/Desktop/my_executable+0xcb48d)
#4 0x55cb8728bb5a in std::__cxx11::_List_base<grpc_impl::Server const*, std::allocator<grpc_impl::Server const*> >::_M_clear() (/home/john/Desktop/my_executable+0xc2b5a)
#5 0x55cb87287307 in std::__cxx11::_List_base<grpc_impl::Server const*, std::allocator<grpc_impl::Server const*> >::~_List_base() (/home/john/Desktop/my_executable+0xbe307)
#6 0x55cb87278d29 in std::__cxx11::list<grpc_impl::Server const*, std::allocator<grpc_impl::Server const*> >::~list() (/home/john/Desktop/my_executable+0xafd29)
#7 0x55cb87278e2c in grpc_impl::CompletionQueue::~CompletionQueue() (/home/john/Desktop/my_executable+0xafe2c)
#8 0x7fe9d1826998 in grpc_impl::Server::SyncRequest::CallData::ContinueRunAfterInterception() (/usr/local/lib/libgrpc++.so.1+0x6f998)
#9 0x7fe9d18278ee in grpc_impl::Server::SyncRequestThreadManager::DoWork(void*, bool, bool) (/usr/local/lib/libgrpc++.so.1+0x708ee)
#10 0x7fe9d182c4ca in grpc::ThreadManager::MainWorkLoop() (/usr/local/lib/libgrpc++.so.1+0x754ca)
#11 0x7fe9d182c68b in grpc::ThreadManager::WorkerThread::Run() (/usr/local/lib/libgrpc++.so.1+0x7568b)
#12 0x7fe9cf5a78d2 in grpc_core::(anonymous namespace)::ThreadInternalsPosix::ThreadInternalsPosix(char const*, void (*)(void*), void*, bool*, grpc_core::Thread::Options const&)::{lambda(void*)#1}::_FUN(void*) (/usr/local/lib/libgpr.so.10+0x118d2)
#13 0x7fe9d1eef6da in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x76da)
#14 0x7fe9d0ba8a3e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0x121a3e)
0x61b00000fcc8 is located 72 bytes inside of 1448-byte region [0x61b00000fc80,0x61b000010228)
allocated by thread T5 (grpcpp_sync_ser) here:
#0 0x7fe9d355f448 in operator new(unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0448)
#1 0x7fe9d18274f2 in grpc_impl::Server::SyncRequestThreadManager::DoWork(void*, bool, bool) (/usr/local/lib/libgrpc++.so.1+0x704f2)
Thread T5 (grpcpp_sync_ser) created by T0 here:
#0 0x7fe9d34b6d2f in __interceptor_pthread_create (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x37d2f)
#1 0x7fe9cf5a7a92 in grpc_core::Thread::Thread(char const*, void (*)(void*), void*, bool*, grpc_core::Thread::Options const&) (/usr/local/lib/libgpr.so.10+0x11a92)
SUMMARY: AddressSanitizer: bad-free (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe12c0) in operator delete(void*)
==14394==ABORTING
None of my code tries to free any pointer and error seems to be coming from some auto generated file only. Please suggest if some more code/details needed.
I briefly checked the error message and code but it looks strange to me because both allocation and destruction were done by C++ new & delete. This is also consistent with your error message.
### Destruction (with operator delete)
#0 0x7fe9d35602c0 in operator delete(void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe12c0)
### Allocation (with operator new)
#0 0x7fe9d355f448 in operator new(unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0448)
This might be caused by other issues like buggy ASAN or customized memory allocator.

Memory leak when linking against opencv

I'm linking a library against opencv libraries. I noticed that when I run the unit test executable (using GTest) for that library, that valgrind reports a memory leak/conditional jump or move depends on uninitialised value, even when the test doesn't do anything at all (just empty test method).
I traced that issue back to a single line in my CMakeLists.txt - when I remove the linkage against opencv, the memory leak goes away. This is the releavant snippet:
find_package(OpenCV REQUIRED)
set(libImageSources src/SourceImageFile.cpp)
add_library(image SHARED ${libImageSources})
# removing this line fixes the leak:
target_link_libraries(image ${OpenCV_LIBS})
Otherwise when runnig an empty unit test, I get an error like this:
==18681== Memcheck, a memory error detector
==18681== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18681== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18681== Command: ./image_test /Users/max/Documents/playground/cpp/image-server/tests
==18681==
==18681== Conditional jump or move depends on uninitialised value(s)
==18681== at 0x7FFF5FC24A27: bcmp (in /usr/lib/dyld)
==18681== by 0x7FFF5FC11904: ImageLoaderMachO::validateFirstPages(linkedit_data_command const*, int, unsigned char const*, unsigned long, long long, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC16B7A: ImageLoaderMachOCompressed::instantiateFromFile(char const*, int, unsigned char const*, unsigned long, unsigned long long, unsigned long long, stat const&, unsigned int, unsigned int, linkedit_data_command const*, encryption_info_command const*, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC10A6E: ImageLoaderMachO::instantiateFromFile(char const*, int, unsigned char const*, unsigned long long, unsigned long long, stat const&, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC038C2: dyld::loadPhase6(int, stat const&, char const*, dyld::LoadContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08468: dyld::loadPhase5(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08188: dyld::loadPhase4(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC07EED: dyld::loadPhase3(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC07642: dyld::loadPhase1(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0347A: dyld::loadPhase0(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0315E: dyld::load(char const*, dyld::LoadContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08705: dyld::libraryLocator(char const*, bool, char const*, ImageLoader::RPathChain const*) (in /usr/lib/dyld)
==18681==
==18681== Use of uninitialised value of size 8
==18681== at 0x7FFF5FC24A3F: bcmp (in /usr/lib/dyld)
==18681== by 0x7FFF5FC11904: ImageLoaderMachO::validateFirstPages(linkedit_data_command const*, int, unsigned char const*, unsigned long, long long, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC16B7A: ImageLoaderMachOCompressed::instantiateFromFile(char const*, int, unsigned char const*, unsigned long, unsigned long long, unsigned long long, stat const&, unsigned int, unsigned int, linkedit_data_command const*, encryption_info_command const*, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC10A6E: ImageLoaderMachO::instantiateFromFile(char const*, int, unsigned char const*, unsigned long long, unsigned long long, stat const&, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC038C2: dyld::loadPhase6(int, stat const&, char const*, dyld::LoadContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08468: dyld::loadPhase5(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08188: dyld::loadPhase4(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC07EED: dyld::loadPhase3(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC07642: dyld::loadPhase1(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0347A: dyld::loadPhase0(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0315E: dyld::load(char const*, dyld::LoadContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08705: dyld::libraryLocator(char const*, bool, char const*, ImageLoader::RPathChain const*) (in /usr/lib/dyld)
==18681==
==18681== Use of uninitialised value of size 8
==18681== at 0x7FFF5FC24A44: bcmp (in /usr/lib/dyld)
==18681== by 0x7FFF5FC11904: ImageLoaderMachO::validateFirstPages(linkedit_data_command const*, int, unsigned char const*, unsigned long, long long, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC16B7A: ImageLoaderMachOCompressed::instantiateFromFile(char const*, int, unsigned char const*, unsigned long, unsigned long long, unsigned long long, stat const&, unsigned int, unsigned int, linkedit_data_command const*, encryption_info_command const*, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC10A6E: ImageLoaderMachO::instantiateFromFile(char const*, int, unsigned char const*, unsigned long long, unsigned long long, stat const&, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC038C2: dyld::loadPhase6(int, stat const&, char const*, dyld::LoadContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08468: dyld::loadPhase5(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08188: dyld::loadPhase4(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC07EED: dyld::loadPhase3(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC07642: dyld::loadPhase1(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0347A: dyld::loadPhase0(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0315E: dyld::load(char const*, dyld::LoadContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08705: dyld::libraryLocator(char const*, bool, char const*, ImageLoader::RPathChain const*) (in /usr/lib/dyld)
==18681==
==18681== Conditional jump or move depends on uninitialised value(s)
==18681== at 0x7FFF5FC11907: ImageLoaderMachO::validateFirstPages(linkedit_data_command const*, int, unsigned char const*, unsigned long, long long, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC16B7A: ImageLoaderMachOCompressed::instantiateFromFile(char const*, int, unsigned char const*, unsigned long, unsigned long long, unsigned long long, stat const&, unsigned int, unsigned int, linkedit_data_command const*, encryption_info_command const*, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC10A6E: ImageLoaderMachO::instantiateFromFile(char const*, int, unsigned char const*, unsigned long long, unsigned long long, stat const&, ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC038C2: dyld::loadPhase6(int, stat const&, char const*, dyld::LoadContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08468: dyld::loadPhase5(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08188: dyld::loadPhase4(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC07EED: dyld::loadPhase3(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC07642: dyld::loadPhase1(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0347A: dyld::loadPhase0(char const*, char const*, dyld::LoadContext const&, std::__1::vector<char const*, std::__1::allocator<char const*> >*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0315E: dyld::load(char const*, dyld::LoadContext const&) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC08705: dyld::libraryLocator(char const*, bool, char const*, ImageLoader::RPathChain const*) (in /usr/lib/dyld)
==18681== by 0x7FFF5FC0E85D: ImageLoader::recursiveLoadLibraries(ImageLoader::LinkContext const&, bool, ImageLoader::RPathChain const&) (in /usr/lib/dyld)
==18681==
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ImageProcessingConfigurationService
[ RUN ] ImageProcessingConfigurationService.evaluateConfigurationFile
[ OK ] ImageProcessingConfigurationService.evaluateConfigurationFile (13 ms)
[----------] 1 test from ImageProcessingConfigurationService (28 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (114 ms total)
[ PASSED ] 1 test.
==18681==
==18681== HEAP SUMMARY:
==18681== in use at exit: 1,346,030 bytes in 1,453 blocks
==18681== total heap usage: 2,926 allocs, 1,473 frees, 3,045,052 bytes allocated
==18681==
==18681== LEAK SUMMARY:
==18681== definitely lost: 10,580 bytes in 137 blocks
==18681== indirectly lost: 10,032 bytes in 152 blocks
==18681== possibly lost: 9,568 bytes in 183 blocks
==18681== still reachable: 43,598 bytes in 520 blocks
==18681== suppressed: 1,272,252 bytes in 461 blocks
==18681== Rerun with --leak-check=full to see details of leaked memory
==18681==
==18681== For counts of detected and suppressed errors, rerun with: -v
==18681== Use --track-origins=yes to see where uninitialised values come from
==18681== ERROR SUMMARY: 36 errors from 4 contexts (suppressed: 0 from 0)
Compiler is clang++ Apple LLVM version 7.3.0 (clang-703.0.31), OS X El Capitan (10.11.1).
Anyone else has had an issue like that? It seems stuff works nontheless, but the error creates noise that I'd rather ignore, I'm not sure if I can fix the root cause.
The issue seems like a false positive reported by valgrind which I can ignore as it's not caused by code.
I tried (at the time of writing) the latest valgrind suppressions file for macOS which didn't work for me, the issue was still reported.
I let valgrind dump the suppressions needed to ignore this error, using a command like:
valgrind --leak-check=full --show-reachable=yes --error-limit=no --gen-suppressions=all --log-file=l.log ./image_test
And then used a tool to parse the log output (the awk script didn't work for me as it contained a syntax error, I used the perl version) into a suppressions file:
cat l.log | ./parse_valgrind_suppressions.pl > s.supp
Now running the test with the generated suppresions file ignores the memory leak:
valgrind --suppressions=./s.supp ./image_test

Uninitialised value was created by a heap allocation in libchilkat?

I run sudo valgrind --leak-check=full -v --track-origins=yes ./myprogram
The result is:
==31734== 1 errors in context 7 of 7:
==31734== Conditional jump or move depends on uninitialised value(s)
==31734== at 0x61CCCC: TlsProtocol::sendRecord(unsigned char const*, unsigned int, int, int, int, TlsEndpoint&, unsigned int, SocketParams&, LogBase&) (in /home/me/src/myprogram)
==31734== by 0x61D32E: TlsProtocol::sendHandshakeMessages(DataBuffer&, int, int, TlsEndpoint&, unsigned int, SocketParams&, LogBase&) (in /home/me/src/myprogram)
==31734== by 0x620B1C: TlsProtocol::sendClientHello(TlsEndpoint&, unsigned int, SocketParams&, LogBase&) (in /home/me/src/myprogram)
==31734== by 0x6265E7: TlsProtocol::clientHandshake2(TlsEndpoint&, unsigned int, SocketParams&, LogBase&) (in /home/me/src/myprogram)
==31734== by 0x627789: TlsProtocol::clientHandshake(char const*, int, TlsEndpoint&, unsigned int, SocketParams&, SharedCertChain*, LogBase&) (in /home/me/src/myprogram)
==31734== by 0x616855: SChannelChilkat::connectImplicitSsl(char const*, int, bool, int, unsigned int, unsigned int, SystemCerts&, SocketParams&, LogBase&, bool&, SharedCertChain*) (in /home/me/src/myprogram)
==31734== by 0x56DDEB: Socket2::connect2(char const*, int, bool, bool, int, SystemCerts&, unsigned int, unsigned int, SocketParams&, LogBase&, bool&, int&, SharedCertChain*) (in /home/me/src/myprogram)
==31734== by 0x56E24F: Socket2::socket2Connect(char const*, int, bool, bool, int, SystemCerts&, unsigned int, unsigned int, SocketParams&, LogBase&, bool&, int&, SharedCertChain*, ClsSocksClient&, ClsHttpProxyClient*) (in /home/me/src/myprogram)
==31734== by 0x514FD9: Imap::connectToImapServer(char const*, int, StringBuffer&, SharedCertChain*, SystemCerts&, ClsSocksClient&, ClsHttpProxyClient*, LogBase&, ProgressMonitor*) (in /home/me/src/myprogram)
==31734== by 0x48BB69: ClsImap::Connect(XString&, ProgressEvent*) (in /home/me/src/myprogram)
==31734== by 0x454A5D: CkImap::Connect(char const*) (in /home/me/src/myprogram)
==31734== by 0x422C2E: EmailSensorAlertConnection::Connect(char const*, int, bool, char const*, char const*) (emailconnection.cpp:150)
==31734== Uninitialised value was created by a heap allocation
==31734== at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31734== by 0x47F219: ClsImap::createNewCls() (in /home/me/src/myprogram)
==31734== by 0x4567EA: CkImap::CkImap() (in /home/me/src/myprogram)
==31734== by 0x422A13: EmailConnection::EmailConnection(std::string) (emailconnection.cpp:110)
==31734== by 0x41D364: IMAPConnectionFactory::getIMAPConnection() (imapconnectionfactory.cpp:46)
==31734== by 0x431362: main (myprogram.cpp:348)
==31734==
==31734== ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 0 from 0)
Part of myprogram.cpp is:
bool connectRes = m_imap.Connect( server );//line 348
I try to find the uninitialized variable. It seems that it is in:ClsImap::createNewCls(). I used grep to search this function. This function is in libchilkat-e/lib/libchilkat-9.5.0.a. How to fix this problem? It seems that the problem is in the lib. So I can do nothing? Am I right?

Invalid free() / delete / delete[] / realloc(), 4 bytes inside a block of size 16 alloc'd [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've written a program using C++. It runs fine until the end, where it vomits: * Error in `./xwd': free(): invalid pointer: 0x00000000017fd774 *
My first thought was that I was doing something wrong in the destructor, but as the relevant class has no destructor as it doesn't use "new", this was a little confusing to me. This led me to use valgrind for the first time, but that hasn't been terribly illuminating.
Valgrind gives the following complaint:
2 errors in context 1 of 1:
==14245== Invalid free() / delete / delete[] / realloc()
==14245== at 0x4C2D2E0: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14245== by 0x407335: __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) (new_allocator.h:110)
==14245== by 0x406429: __gnu_cxx::__alloc_traits<std::allocator<int> >::deallocate(std::allocator<int>&, int*, unsigned long) (alloc_traits.h:185)
==14245== by 0x404C07: std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) (stl_vector.h:178)
==14245== by 0x404796: std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&) (vector.tcc:394)
==14245== by 0x402C21: std::vector<int, std::allocator<int> >::push_back(int const&) (stl_vector.h:925)
==14245== by 0x40186C: Lexicon::Lexicon(char const*, unsigned int) (Lexicon.cpp:36)
==14245== by 0x40B58A: main (xwd.cpp:20)
==14245== Address 0x5a65114 is 4 bytes inside a block of size 16 alloc'd
==14245== at 0x4C2C100: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14245== by 0x40809D: __gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*) (new_allocator.h:104)
==14245== by 0x4072B3: __gnu_cxx::__alloc_traits<std::allocator<int> >::allocate(std::allocator<int>&, unsigned long) (alloc_traits.h:182)
==14245== by 0x4061CF: std::_Vector_base<int, std::allocator<int> >::_M_allocate(unsigned long) (stl_vector.h:170)
==14245== by 0x404684: std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&) (vector.tcc:353)
==14245== by 0x402C21: std::vector<int, std::allocator<int> >::push_back(int const&) (stl_vector.h:925)
==14245== by 0x40186C: Lexicon::Lexicon(char const*, unsigned int) (Lexicon.cpp:36)
==14245== by 0x40B58A: main (xwd.cpp:20)
==14245==
==14245== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 0 from 0)
This points to Lexicon.cpp (http://pastebin.com/Me4AcwXx) ,line 36, which is the second-to-last line of the following code sample:
for(unsigned int letter = 0; letter < next.length(); letter++){
assert(0<=pattern.length()&&pattern.length()<=index_vectors.size());
assert(0<=letter && letter<=index_vectors[pattern.length()].size());
assert(0<=pattern[letter]-'A' && pattern[letter]-'A'<=index_vectors[pattern.length()][letter].size());
index_vectors[next.length()][letter][next[letter]-'A'].push_back(word_list_index);
full_letter_vectors[next.length()].push_back(word_list_index); }
Here, word_list_index is simply an int. What's wrong with this picture? Am I doing something heinous? It all looks pretty normal to me.
Solution:
Thanks to everyone who suggestion I put in asserts (god, grad school has turned me into a TERRIBLE programmer), which led me to catch the problem: one of the words in the dictionary was "MP3" which wasn't entirely alphanumeric. I didn't realize that that kind of error would wait until deconstruction to pop up; I assumed it would cause a segfault. YIKES.
Thanks all!