Tag Archives: c++

[Solved] JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring

JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring

A/k.myapplicatio: java_vm_ext.cc:545] JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring
    java_vm_ext.cc:545]     in call to GetStringUTFChars
    java_vm_ext.cc:545]     from void com.sdk.myapplication.MainActivity.triggerGetStringUTFCharsNPE(java.lang.String, java.lang.String)

The reason for the above message, from a Jni method, refers to the case where the JVM calls the method and the GetStringUTFChars entry jstring is empty, it must not refer specifically to the method body where the GetStringUTFChars call has a NULl pointer, but includes other C/C++ method calls in the method that have a NULL pointer. NULL pointer. So we have to look at the crash stack carefully to quickly locate the place where the NULL pointer is generated to avoid invalid output.

the above example is extracted according to our own business code
the example code is as follows
java

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        triggerGetStringUTFCharsNPE("hello", null);//introduce a null
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();


    public native void triggerGetStringUTFCharsNPE(String p1, String p2);
}

jni

#include <jni.h>
#include <string>
#include <android/log.h>

static const char* TAG = "Demo";


void nestFunction(JNIEnv *env, jstring p2) {
    //Here p1 is simply passed in via a parameter, but the actual business logic may be obtained by other methods/ways
    const char* pStr = env->GetStringUTFChars(p2, nullptr);
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "pStr:%s", pStr);
    env->ReleaseStringUTFChars(p2, pStr);
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_sdk_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}extern "C"
JNIEXPORT void JNICALL
Java_com_sdk_myapplication_MainActivity_triggerGetStringUTFCharsNPE(JNIEnv *env, jobject thiz,
                                                                    jstring p1, jstring p2) {
    const char* pStr1 = env->GetStringUTFChars(p1, nullptr);
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "pStr1:%s", pStr1);
    env->ReleaseStringUTFChars(p1, pStr1);
    //故意让nestFunction产生 JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring
    nestFunction(env, p2);
}

How to Solve Microsoft.CppCommon.targets(279,5): error MSB3073

1>*** PARSE FAILURE ***
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(279,5): error MSB3073: The command "@echo off
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(279,5): error MSB3073: setlocal...

There is no problem with local compilation, and the compilation server reports an error.

The reason is the problem of GIT line feed in Windows system. The carriage return line feed in the project is modified in the vcxproj obtained by git, resulting in the script error in vcxproj.

Modify the GIT line feed setting of windows system, and the problem disappears.

[Solved] std::max() error C2589: ‘(‘ : illegal token on right side of ‘::‘

int max =std::numeric_limits< int>: max();
:: Based on error indicating:

f:\code\cpp\webspider\main. cpp(47): warning C4003: not enough actual parameters for macro ‘max’

f:\code\cpp\webspider\main. cpp(47) : error C2589: ‘(‘ : illegal token on right side of ‘::’

f:\code\cpp\webspider\main. cpp(47) : error C2059: syntax error : ‘::’

Cause: STL’s numeric_limits::max() and VC6 min/max macro conflict.

The problem should be that the macro definitions of the above two header files conflict.

Solution: Use parentheses “()” to avoid precompiler errors. int max = (std::numeric_limits<std::streamsize>::max)(); That’s it.

[Solved] CMake Error Cannot specify link libraries for target “variational“ which is not built by this projec

For errors:

CMake Error at D:\VSA_paper\VSA\CMakeLists.txt:48 (target_link_libraries):
1> [CMake] Cannot specify link libraries for target “variational” which is not built by this project.

 

All the information found said target_link_libraries is not placed behind add_executable, but not mine
the following is the code segment where the error is located

add_executable(${PROJECT_NAME}_bin ${SRCFILES}  ${HEAD_FILES} ${RCC_FILES} ${WRAP_FILES}  "gauss.h" "developable_degree.h")
target_link_libraries(${PROJECT_NAME} Qt5::Widgets Qt5::Core Qt5::Gui)
target_link_libraries(${PROJECT_NAME}_bin igl::core igl::opengl_glfw)
target_link_libraries(${PROJECT_NAME}_bin OpenMesh)

From the error “Cannot specify link libraries for target “variational” which is not built by this project.” You can see that the project name “variant” is wrong and should be “variant_bin”.

The solution is to add the missing “_bin” after ${project_name} in the above code segment. The modified result is as follows:

add_executable(${PROJECT_NAME}_bin ${SRCFILES}  ${HEAD_FILES} ${RCC_FILES} ${WRAP_FILES}  "gauss.h" "developable_degree.h")
#add Qt5
target_link_libraries(${PROJECT_NAME}_bin Qt5::Widgets Qt5::Core Qt5::Gui)
target_link_libraries(${PROJECT_NAME}_bin igl::core igl::opengl_glfw)
target_link_libraries(${PROJECT_NAME}_bin OpenMesh)

[Solved] error c101008d: Failed to write the updated manifest to the resource of file

System: win10
compiler: vs08

Error c101008d: error c101008d: Failed to write the updated manifest to the resource of file “XXX.exe”

Solution: Microsoft menu bar in the lower left corner – Settings – update and security – Windows Security Center – virus and threat protection – virus and threat protection settings – the following management settings – implement protection – off – recompile the project

[Solved] Centos error: collect2: fatal error: cannot find ‘ld‘

When executing a compiled file in Linux (. /configure) with collect2: fatal error: cannot find ‘ld’, which is also preceded by gcc: error: unrecognized command line option -V and configure: error: C compiler cannot create executables. When you find that your gcc and the corresponding dependency packages are installed, you still get the error. At this point you look under /usr/bin/ to see if there is an ld. which ld is not found, but you find a file called ld.gold under /usr/bin by running ls -l |grep ld.

Solution:

Copy ld.gold under /usr/bin/ (do not delete the original file) and rename it to ld. You can also create a soft connection.

Re-execute the compilation

Problem found solved!

Brief description.

gcc compiles the source code to .o, then linker links the .o to .so or executable, linker can use ld.bfd, ld.gold or lld.

[Solved] Linux gcc Compile Error: unknown type name ‘bool‘

Error description

error: unknown type name ‘bool’; did you mean ‘_Bool’?

Cause of error
Unknown type name: ‘bool’, because the boolean type is not defined in the C standard (C89), so it will report an error. C99 provides a header file that defines bool, true for 1 and false for 0. Just import stdbool.h and you can manipulate boolean types very easily.

Solution:

//add the header file 
#include <stdbool.h>

Failed to find Qt component “Widgets“ [How to Solve]

In the project, QT6 is used 2.1 after installing QT, this error is reported when QtCreator is used:

but it set Qt6_FOUND to FALSE so package "Qt6" is considered to be NOT
  FOUND.  Reason given by package:

  Failed to find Qt component "Widgets" config file at ""

The reason is that although QT has been installed, some other libraries QT depends on have not been installed:

I use Ubuntu system: use the following command to install (OpenGL installation)

sudo apt-get install build-essential libgl1-mesa-dev

[Solved] error: link.exe‘ failed with exit status 1158

Error Messages:

bug error: command ‘\XXX\VS2015\VC\BIN\x86_amd64\link.exe‘ failed with exit status 1158

 

Solution:

Because some of the previous system configurations are gone, reinstall things such as vs and buildtools, but there are many problems in the installation process, and there are always missing things. Record the contents that are difficult to find here
prompt error when the program is running: error: command ‘\XXX\VS2015\VC\BIN\x86_amd64\link.exe‘ failed with exit status 1158
background display
I read the online solution, that is, vs is missing two files rc.exe and rcdl.dll can’t run, but I can’t find these two files according to the online path
the path of others is:
C:\Program Files(×86)Windows Kits\8.1\bin\×86
I found it in this path
C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64
or
C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x86
both can be used
in short, these two files are rc.exe andrcdl.dll must be in the path of C:\Program Files (x86)\Windows Kits, but the location of different system configurations may be different. This is probably related to some things you installed. Look carefully

then copy the two files to
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64

Or C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin
you can solve the problem by installing Microsoft Visual Studio 14.0

Leetcode error: AddressSanitizer:DEADLYSIGNAL [How to Solve]

leetcode error:

AddressSanitizer:DEADLYSIGNAL ================================================================= ==43==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x00000034ca17 bp 0x7ffe82f08070 sp 0x7ffe82f07f40 T0) ==43==The signal is caused by a READ memory access. ==43==Hint: this fault was caused by a dereference of a high value address (see register values below). Dissassemble the provided pc to learn which register was used. #3 0x7f952a7800b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) AddressSanitizer can not provide additional info. ==43==ABORTING

As you can see, the unknown address was accessed.
Reason:
The code accesses the top element of the stack for an empty stack

else if(s[i] == '}')
{
    if(st.top() == '{')
    {
       st.pop();
    }else
    {
       return false;
    }
}

Solution:

Just join the blank judgment

else if(s[i] == '}')
{
    if(!st.empty() && st.top() == '{')
    {
        st.pop();
    }else
    {
        return false;
    }
}

[Solved] PCL Compile Error: undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>:: XXX

Environment:
Ubuntu 20.04, PCL 1.11.0
when compiling PCL with cmake, an error is reported:

/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE[_ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE]+0x20): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setInputCloud(std::shared_ptr<pcl::PointCloud<pcl::PointXYZRGBA> const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE[_ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE]+0x28): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<std::vector<int, std::allocator<int> > > const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE[_ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE]+0x30): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<std::vector<int, std::allocator<int> > const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE[_ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE]+0x38): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<pcl::PointIndices const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE[_ZTVN3pcl7PCLBaseINS_12PointXYZRGBAEEE]+0x40): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(unsigned long, unsigned long, unsigned long, unsigned long)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl6FilterINS_12PointXYZRGBAEEE[_ZTVN3pcl6FilterINS_12PointXYZRGBAEEE]+0x20): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setInputCloud(std::shared_ptr<pcl::PointCloud<pcl::PointXYZRGBA> const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl6FilterINS_12PointXYZRGBAEEE[_ZTVN3pcl6FilterINS_12PointXYZRGBAEEE]+0x28): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<std::vector<int, std::allocator<int> > > const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl6FilterINS_12PointXYZRGBAEEE[_ZTVN3pcl6FilterINS_12PointXYZRGBAEEE]+0x30): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<std::vector<int, std::allocator<int> > const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl6FilterINS_12PointXYZRGBAEEE[_ZTVN3pcl6FilterINS_12PointXYZRGBAEEE]+0x38): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<pcl::PointIndices const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl6FilterINS_12PointXYZRGBAEEE[_ZTVN3pcl6FilterINS_12PointXYZRGBAEEE]+0x40): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(unsigned long, unsigned long, unsigned long, unsigned long)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE[_ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE]+0x20): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setInputCloud(std::shared_ptr<pcl::PointCloud<pcl::PointXYZRGBA> const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE[_ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE]+0x28): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<std::vector<int, std::allocator<int> > > const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE[_ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE]+0x30): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<std::vector<int, std::allocator<int> > const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE[_ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE]+0x38): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(std::shared_ptr<pcl::PointIndices const> const&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE[_ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE]+0x40): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setIndices(unsigned long, unsigned long, unsigned long, unsigned long)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/voslam.cpp.o:(.data.rel.ro._ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE[_ZTVN3pcl9VoxelGridINS_12PointXYZRGBAEEE]+0x48): undefined reference to `pcl::VoxelGrid<pcl::PointXYZRGBA>::applyFilter(pcl::PointCloud<pcl::PointXYZRGBA>&)'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/pointcloudmapping.cpp.o: in function `PointCloudMapping::PointCloudMapping(double)':
pointcloudmapping.cpp:(.text+0x1ac): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::PCLBase()'
/usr/bin/ld: CMakeFiles/MYSLAM.dir/src/pointcloudmapping.cpp.o: in function `PointCloudMapping::viewer()':
pointcloudmapping.cpp:(.text+0x1626): undefined reference to `pcl::visualization::CloudViewer::CloudViewer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: pointcloudmapping.cpp:(.text+0x1d39): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::setInputCloud(std::shared_ptr<pcl::PointCloud<pcl::PointXYZRGBA> const> const&)'
/usr/bin/ld: pointcloudmapping.cpp:(.text+0x1d66): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::initCompute()'
/usr/bin/ld: pointcloudmapping.cpp:(.text+0x1db5): undefined reference to `pcl::VoxelGrid<pcl::PointXYZRGBA>::applyFilter(pcl::PointCloud<pcl::PointXYZRGBA>&)'
/usr/bin/ld: pointcloudmapping.cpp:(.text+0x1dbd): undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>::deinitCompute()'
/usr/bin/ld: pointcloudmapping.cpp:(.text+0x1ff7): undefined reference to `pcl::visualization::CloudViewer::showCloud(std::shared_ptr<pcl::PointCloud<pcl::PointXYZRGBA> const> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: pointcloudmapping.cpp:(.text+0x24dd): undefined reference to `pcl::VoxelGrid<pcl::PointXYZRGBA>::applyFilter(pcl::PointCloud<pcl::PointXYZRGBA>&)'
/usr/bin/ld: pointcloudmapping.cpp:(.text+0x263d): undefined reference to `pcl::visualization::CloudViewer::~CloudViewer()'
/usr/bin/ld: pointcloudmapping.cpp:(.text+0x2798): undefined reference to `pcl::visualization::CloudViewer::~CloudViewer()'
collect2: error: ld returned 1 exit status

Solution:
manually specify the header file and link library of PCL:

# find_package(PCL 1.11.0 EXACT REQUIRED) 
# add_definitions( ${PCL_DEFINITIONS} )

-------Modify---:
file(GLOB PCL_LIBRARIES /usr/local/lib/libpcl_*)
set(PCL_INCLUDE_DIRS /usr/local/include/pcl-1.11)

INCLUDE_DIRECTORIES(${PROJECT_NAME} ${PCL_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PCL_LIBRARIES})