PCL 1.8.1 crash on cloud delete after StatisticalOutliersRemoval - memory

I have an application which is build using several DLL files.
I'm trying to perform PCL's statistical outliers removal using the following code:
PointCloudWithRGBNormalsPtr pclCloud(new PointCloudWithRGBNormals());
ConvertPointCloudToPCL(in_out_cloud /*my own structure which includes xyz, rgb, nx ny nz*/, *pclCloud);
pcl::StatisticalOutlierRemoval<PointXYZRGBNormal> sor;
sor.setInputCloud(pclCloud);
sor.setMeanK(10);
sor.setStddevMulThresh(1.0);
sor.filter(*pclCloud);
ConvertPointCloudToPCL:
static void ConvertPointCloudToPCL(const std::vector<Cloud3DrgbN> &in, PointCloudWithRGBNormals &output)
{
for (auto it = in.begin(); it != in.end(); it++)
{
const Cloud3DrgbN &p3d = *it;;
PointXYZRGBNormal p;
p.x = p3d.x;
p.y = p3d.y;
p.z = p3d.z;
p.normal_x = p3d.nX;
p.normal_y = p3d.nY;
p.normal_z = p3d.nZ;
p.r = p3d.r;
p.g = p3d.g;
p.b = p3d.b;
output.push_back(p);
}
}
For some reason, if I call this function from 1 of my dlls it works as it should. However, there's 1 dll that if I call it from it, when pclCloud goes out of scope, I'm getting an exception from Eigen's Memory.h file at the handmade_aligned_free function
I'm using Windows 10 64-bit, pcl 1.8.1 and Eigen 3.3 (tried 3.3.4, same thing)
Update:
After further digging, I've found that EIGEN_MALLOC_ALREADY_ALIGNED was set to 0 because I'm using AVX2 in my "problematic" DLL. I'm still not sure though why using Eigen's "handmade" aligned malloc/free causes this crash.
There seems to be a known issue (see this) with Eigen, PCL & AVX

Well I found the problem and how to solve it.
It seems that the DLLs that comes with the "All in 1 installer" for windows weren't compiled with AVX/AVX2 support.
When linking these libraries with my own DLLs, the ones that compiled using the AVX, this mismatch caused Eigen to use the different types of allocations and freeing of memory causing the crash.
I compiled PCL from source using AVX2 and linked these library and everything works.
It's worth mentioning that the DLL that worked before now has issues since it doesn't have AVX and PCL now do.

Related

msvc trying to compile cv::Matx<float,3,1> as a 4-element vector

Using MSVC 2017, OpenCV 3.4. Code
typedef Vec3f localcolor;
inline double lensqd(const localcolor & c) {
return c.ddot(c);
}
Get
error C2338: Matx should have at least 4 elements. channels >= 4
note: while compiling class template member function 'cv::Matx<float,3,1>::Matx(_Tp,_Tp,_Tp,_Tp)'
when compiling the ddot function.
The compiler is trying to instantiate a 3-element vector with 4 initializers. I can't see anything in the OCV source code that would make this happen.
So do I file a bug report with MS?
And how do you suggest I get a working build? The code is this way because I sometimes want
typedef Vec4f localcolor;
which BTW compiles without error.
Could you show the ddot function ?
I recently had the same error by attempting to initialize a Vec3f with 4 elements.

Android: linking to opencv results in SIGBUS (signal SIGBUS: illegal alignment) when exception is thrown

I have to work with opencv in an android project. Everything worked fine until I recently had to use c++ exception_ptr as well.
Since then, the use of std::rethrow_exception causes a SIGBUS (signal SIGBUS: illegal alignment).
I created a minimal example to illustrate the problem. The example application only links to opencv 3.4.4 but does not use any opencv function. If you remove the linking to opencv in CMakeLists.txt the app works fine and doesn't crash. If you add it however, the app will crash as soon as the native method triggerException() is called.
In my implementation the example application calls this method if a button is pressed.
native-lib.cpp:
#include <jni.h>
#include <string>
#include <exception>
/*
* code based on: https://en.cppreference.com/w/cpp/error/exception_ptr
*/
std::string handle_eptr2(std::exception_ptr eptr)
{
try {
if (eptr) {
std::rethrow_exception(eptr);
}
} catch (const std::exception &e) {
return "Caught exception \"" + std::string(e.what()) + "\"\n";
}
return "Something went wrong";
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_user_exceptiontest_MainActivity_triggerException(
JNIEnv *env,
jobject /* this */) {
std::exception_ptr eptr;
try {
std::string().at(1); // this generates an std::out_of_range
} catch(...) {
eptr = std::current_exception(); // capture
}
std::string res = handle_eptr2(eptr);
return env->NewStringUTF(res.c_str());
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
set(OPENCV_DIR $ENV{HOME}/lib/OpenCV-android-sdk/sdk )
include_directories(${OPENCV_DIR}/native/jni/include )
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp)
find_library( log-lib
log)
target_link_libraries(
native-lib
# Removing the following line will make everything work as expected (what() message is returned)
${OPENCV_DIR}/native/libs/${ANDROID_ABI}/libopencv_java3.so # <--- critical line
${log-lib})
build.gradle
To use exceptions and c++17 support, I added the following lines to the configuration that is created by android-studio.
externalNativeBuild {
cmake {
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_STL=c++_shared'
cppFlags "-std=c++1z -frtti -fexceptions"
}
}
Stacktrace:
<unknown> 0x004c4e47432b2b01
___lldb_unnamed_symbol15856$$libopencv_java3.so 0x0000007f811c4a58
_Unwind_Resume_or_Rethrow 0x0000007f811c4fc8
__cxa_rethrow 0x0000007f81181e50
__gnu_cxx::__verbose_terminate_handler() 0x0000007f811b1580
__cxxabiv1::__terminate(void (*)()) 0x0000007f81181c54
std::terminate() 0x0000007f81181cc0
std::rethrow_exception(std::exception_ptr) 0x0000007f802db2cc
handle_eptr2(std::exception_ptr) native-lib.cpp:35
::Java_com_example_user_exceptiontest_MainActivity_triggerException(JNIEnv *, jobject) native-lib.cpp:58
While searching for a solution I looked at the opencv sources (https://github.com/opencv/opencv/blob/master/modules/core/src/parallel.cpp) and stumbled upon this code snippet:
#ifndef CV__EXCEPTION_PTR
# if defined(__ANDROID__) && defined(ATOMIC_INT_LOCK_FREE) && ATOMIC_INT_LOCK_FREE < 2
# define CV__EXCEPTION_PTR 0 // Not supported, details: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58938
I'd understand if this changes the behavior of opencv, but I don't get how this might influence code that does not use opencv at all.
EDIT: It is also worth mentioning that linking to opencv has no impact if I use this code directly (without jni) in a linux (x86_64) desktop setting (clang, libc++, opencv3.4.4). Thus, my conclusion that it is an android specific problem...
Does anyone has an idea how to solve that issue or what to try next?
Thanks a lot in advance!
Opencv is compiled with gnu runtime while you are using c++ stl. See One STL per app. You will need to either use gnustl (you will need to go back to ndk 15 for that) or build opencv with c++ stl.
In order to build opencv with c++_static you can try to follow comment in opencv bugtracker
cmake -GNinja -DINSTALL_ANDROID_EXAMPLES=ON
-DANDROID_EXAMPLES_WITH_LIBS=ON -DBUILD_EXAMPLES=ON -DBUILD_DOCS=OFF -DWITH_OPENCL=OFF -DWITH_IPP=ON -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
-DANDROID_TOOLCHAIN=clang "-DANDROID_STL=c++_static" -DANDROID_ABI=x86 -DANDROID_SDK_TARGET=18 ../opencv
Followed by
make && make install

opencv has triggered a breakpoint Invalid address specified to RtlValidateHeap

I wrote some code in C++ and OpenCV 2.49 in Visual Studio 2013:
cv::Mat mask = Mat::zeros(tmp.size(), CV_8UC1);
modify mask that some elements are zeros and some elements are 255's
cv::vector<cv::vector<cv::Point> > contours;
cv::vector<cv::Vec4i> hierarchy;
cv:findContours(mask, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
cv::imshow("Mask", mask);
When the method ends, following error appears:
Invalid address specified to RtlValidateHeap( 004D0000, 02EBD9D8 )
Project.exe has triggered a breakpoint.
I searched at stackoverflow and other platforms but can't find an answer!
I added the correct opencv path to the "path" environment variable: C:\opencv\build\x86\vc12\bin
and also to the project configurations:
C:\opencv\build\x86\vc12\lib
If I use merge/split from the opencv api it causes the same error, but in this case it is possible to allocate ther vector before going into the method. Then it works. In this case it's not possible to allocate the vector before, because i don't now the size.
I think the problem is that opencv allocates the memory and that's not in the correct heap, but i checked all dll's and paths. Do you have any other ideas?
Thanks in advance!
EDIT:
I replaced the opencv folder with a new downloaded opencv and it works, don't ask me why!
Is the cv::vector declaration valid? You should try std::vector instead of them.
Based on the given code, it is hard to decide what is causing the trouble. Could you provide more details?
Also note that this "function modifies the image while extracting the contours".
You should also check the properties below:
Right click on the Project
Properties - Configurational Properties - General: set Plattform Toolset to Visual Studio 2013 (v120)
Configurational Properties - C/C++ - Code Generation - set Runtime Library:
Multi-threaded Debug DLL (/MDd) for debug builds or Multi-threaded
DLL (/MD) for release build

Microsoft Script Control 64 bit?

Is there any msscript control in 64 bit?
I google a bit and all say no 64-bit yet
The reason that I need 64bit msscript.ocx is that I want to compile delphi projects in 64-bit using XE3.
It compiles OK in XE3 and I have obtained a 64-bit exe but when it executes to the following line,
script := TScriptControl.Create(nil);
It gives me a 'Class Not Registered' error. I only found msscript.ocx under C:\windows\SysWOW64 but there is no such file under System32 folder.
I really want this to work so any quick replacement for this?
This is an old post. but I just found a very good alternative to 64-bit MSScript Control (Microsoft does not have 64-bit msscript.ocx)
http://www.eonet.ne.jp/~gakana/tablacus/scriptcontrol_en.html
and I have changed only a few lines of code in my application and it works in 64-bit based on this ScriptControl64.
The msscript component was not ported to 64 bit. It's a legacy component and MS chose not to put the effort into migrating it to 64 bit. You'll simply need to find another way to do whatever it is you do with that component.
I faced the same issue porting an c++ application from 32 to 64 Bit.
I know that this question was raised on Delphi but I hope someone can make use of this information or transfer it to other languages.
We where initiating the "ScriptControl" (MSScriptControl.ScriptControl.1) via CreateDispatch.
The control info is located in Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\ScriptControl
and we executed VBS or JScript with this MS control in 32 Bit:
COleDispatchDriver* m_dispScriptControl = new COleDispatchDriver();
if (m_dispScriptControl)
{
if (m_dispScriptControl->CreateDispatch(_T("ScriptControl")))
{
...
.... setting language to be used and other proteries .. then execute script code:
_variant_t varResult;
VariantClear(&varResult);
EXCEPINFO excepinfo;
VARIANT parameters;
parameters.vt = VT_BSTR;
parameters.bstrVal = strScriptCodeWCHAR;
DISPPARAMS dispparams;
dispparams.rgdispidNamedArgs = NULL;
dispparams.cNamedArgs = 0;
dispparams.cArgs = 1;
dispparams.rgvarg = &parameters;
unsigned int uArgErr = 0;
if (S_OK != m_dispScriptControl->m_lpDispatch->Invoke(0x7d2, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparams, &varResult, &excepinfo, &uArgErr))
...
}
After some research it seems not to be possible to create the
ScriptControl in 64 bit application as of a MSDN query:
Question was: I have window forms application currently working fine
on 32 Bit Application and i am investigation so we can install it on a
64 bit computer but getting on lauching the application as below.
Class not registered (Exception from HRESULT: 0x80040154
(REGDB_E_CLASSNOTREG))
The error is located on AxInterop.MSScriptControl.dll
Awnser is: If it ONLY lives in C:\Windows\SysWOW64, then your .Net
application cannot run in 64-bit mode. Make sure you compile it for
x86 instead of Any CPU. Then you'll be able to use it in 64-bit
Windows, but it will be a 32-bit process.
https://social.msdn.microsoft.com/Forums/windows/en-US/1e9ddfe4-3408-4a34-ba43-a1a0931daebd/64-bit-windows-7?forum=clr
With which we where not happy as we want to run as 64-bit process
Our solution was to use the Microsoft IActiveScript Interface. And implemented the same on our main window:
BEGIN_INTERFACE_PART(ActiveScriptSite, IActiveScriptSite)
STDMETHOD(GetLCID)(LCID*);
STDMETHOD(GetItemInfo)(LPCOLESTR, DWORD, LPUNKNOWN*, LPTYPEINFO*);
STDMETHOD(GetDocVersionString)(BSTR*);
STDMETHOD(OnScriptTerminate)(const VARIANT*, const EXCEPINFO*);
STDMETHOD(OnStateChange)(SCRIPTSTATE);
STDMETHOD(OnScriptError)(IActiveScriptError*);
STDMETHOD(OnEnterScript)();
STDMETHOD(OnLeaveScript)();
END_INTERFACE_PART(ActiveScriptSite)
Now when we have to execute script code we do the following, which works fine in 64 and 32 BIT versions of our built:
CString strLanguage;
if (nLanguage == 1)
{
strLanguage = _T("VBScript");
}
else
{
strLanguage = _T("JScript");
}
CComPtr<IActiveScript> m_pAxsScript;
HRESULT hr = m_pAxsScript.CoCreateInstance(CT2W(strLanguage), NULL, CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER);
m_pAxsScript->SetScriptSite(&m_xActiveScriptSite); //m_xActiveScriptSite is a member of the interface
CComQIPtr<IActiveScriptParse> m_pAxsScriptParse = m_pAxsScript;
m_pAxsScriptParse->InitNew();
EXCEPINFO pException = { 0 };
hr = m_pAxsScriptParse->ParseScriptText(_bstr_t(strCode), 0, NULL, NULL, dw, 0, 0, NULL, &pException);
//execute script
m_pAxsScript->SetScriptState(SCRIPTSTATE_CONNECTED);
m_pAxsScriptParse.Release();
m_pAxsScriptParse = nullptr;
hr = m_pAxsScript->SetScriptState(SCRIPTSTATE_DISCONNECTED);
ASSERT(hr == S_OK);
m_pAxsScript->Close();
m_pAxsScript.Release();

opencv error: Null pointer(Null filename) in unknown function with facedetect. cpp

OpenCV includes face detection sample code. I tried to use facedetect.cpp from OpenCV 2.2 library in my application. However, I happened to a runtime problem
OpenCV error: Null pointer(Null filename) in unknown function, file..\..\..\..\ocv\opencv\modules\core\src\persistence.cpp,line 2571
I googled it and found lots of people ran across this problem, but without a good solution. This problem happened when code load .xml file. My solution is here:
const char *cascadeNameChar=cascadeName.c_str();
const char *nestedCascadeNameChar=nestedCascadeName.c_str();
......
if( !cvLoad( nestedCascadeNameChar ) )
if(!cascade.load(cascadeName))
Then it works for me. I run it on Visual Studio 2008.
Mates, I have figured out this problem. I used opencv2.2+Visual Studio 2008 when I happened to this problem. The Opencv2.2 is binary version I downloaded from http://sourceforge.net that have been compiled with respect to vc2010. I just now downloaded vc2010 and try my application with facedetect.cpp from opencv2.2 on it. It works! The runtime error with persistence.cpp is gone.

Resources