From b6c85a1ee67ed516db7df4041ddff3e14f9c04da Mon Sep 17 00:00:00 2001 From: cmhu <2657262686@qq.com> Date: Fri, 21 Feb 2025 15:48:15 +0800 Subject: [PATCH] 修复jni中解码失败导致的内存泄露问题 --- jni/VehicleNativeInterface.cpp | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------------------- 1 file changed, 95 insertions(+), 99 deletions(-) diff --git a/jni/VehicleNativeInterface.cpp b/jni/VehicleNativeInterface.cpp index 7e0625d..080639a 100644 --- a/jni/VehicleNativeInterface.cpp +++ b/jni/VehicleNativeInterface.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "../src/village_pic_interface.h" @@ -17,6 +18,7 @@ //dvpp���� #include "dvpp_process.h" #include "utils.h" +#include "logger.hpp" //#include "img_codec.h" double msecond() { @@ -111,6 +113,11 @@ sy_command global_vehicle_manned_config; //�Ƿ������� //sy_command global_vehicle_pose_config; //�Ƿ�����pose��� //20210618 atlas int gpu_id; +DvppProcess* dvpp = nullptr; +aclrtContext ctx = nullptr; +aclrtStream stream = nullptr; + +std::mutex m_single_mtx; /* * Class: com_objecteye_nativeinterface_vehicle_VehicleNativeInterface @@ -151,16 +158,20 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI param.log_days = aiEngineParam_logDays; param.log_mem = aiEngineParam_logMem; - printf("jni dev_id:%d \n", param.dev_id); - printf("jni sdk_path:%s_\n", param.sdk_path.c_str()); - printf("jni log_level:%d\n", param.log_level); - printf("jni log_path:%s\n", param.log_path.c_str()); - printf("jni log_days:%d\n", param.log_days); - printf("jni log_mem:%ld\n", param.log_mem); + + string log_path = param.log_path + "/jni.log"; + set_default_logger(LogLevel(param.log_level), "PicAnalysis", log_path.c_str(), param.log_mem, param.log_days); + + LOG_INFO("jni dev_id:{}", param.dev_id); + LOG_INFO("jni sdk_path:{}", param.sdk_path.c_str()); + LOG_INFO("jni log_level:{}", param.log_level); + LOG_INFO("jni log_path:{}", param.log_path.c_str()); + LOG_INFO("jni log_days:{}", param.log_days); + LOG_INFO("jni log_mem:{}", param.log_mem); int ret = village_pic_init(&vaHandle, param); if (ret != SUCCESS) { - printf("jni info:va_init failed."); + LOG_ERROR("jni village_pic_init failed."); return ret; } @@ -182,33 +193,21 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI env->SetLongArrayRegion(handle, 0, 1, temp); } env->ReleaseStringUTFChars(str_sdk_path, sdk_path); - return ret; -} + env->ReleaseStringUTFChars(str_aiEngineParam_logPath, aiEngineParam_logPath); + ACL_CALL(aclrtCreateContext(&ctx, gpu_id), ACL_ERROR_NONE, FAILED); + ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, FAILED); + ACL_CALL(aclrtCreateStream(&stream), ACL_ERROR_NONE, FAILED); + + dvpp = new DvppProcess(); + dvpp->InitResource(stream); -void CvBGR2NV21(cv::Mat& bgr, unsigned char* yuv) { - int stride = (bgr.cols + 127) / 128 * 128; - int strideH = (bgr.rows + 15) / 16 * 16; - for (int i = 0; i < bgr.rows; i++) { - for (int j = 0; j < bgr.cols; j++) { - int B = bgr.at(i, j)[0]; - int G = bgr.at(i, j)[1]; - int R = bgr.at(i, j)[2]; + LOG_ERROR("jni init succeed."); - int Y = (77 * R + 150 * G + 29 * B) >> 8; - yuv[i * stride + j] = (Y < 0) ? 0 : ((Y > 255) ? 255 : Y); - if (i % 2 == 0 && j % 2 == 0) { - int U = ((-44 * R - 87 * G + 131 * B) >> 8) + 128; - int V = ((131 * R - 110 * G - 21 * B) >> 8) + 128; - yuv[strideH * stride + i / 2 * stride + j] = (V < 0) ? 0 : ((V > 255) ? 255 : V); - yuv[strideH * stride + i / 2 * stride + j + 1] = (U < 0) ? 0 : ((U > 255) ? 255 : U); - } - } - } + return ret; } - /* * Class: com_objecteye_nativeinterface_vehicle_VehicleNativeInterface * Method: batch @@ -218,35 +217,25 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI { int ret = FAILED; void *vaHandle = (void *)handle; + if (nullptr == vaHandle || nullptr == dvpp || nullptr == stream || nullptr == ctx) + { + return ret; + } + + std::lock_guard l(m_single_mtx); - // ��ȡͼƬ���� jclass cls_syImgParam = env->FindClass("com/objecteye/pojo/common/SyImgParam"); jfieldID fid_image_str = env->GetFieldID(cls_syImgParam, "image_string", "Ljava/lang/String;");//base64�����ⲿͳһ��url��fileת��base64���� sy_img batch_img[batchSize]; - cv::Mat car_images[batchSize]; ImageData dvpp_data[batchSize]; - aclrtContext ctx; - ACL_CALL(aclrtCreateContext(&ctx, gpu_id), ACL_ERROR_NONE, FAILED); ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, FAILED); - //��ʼ��dvpp - aclrtStream stream = nullptr; - ACL_CALL(aclrtCreateStream(&stream), ACL_ERROR_NONE, FAILED); - - DvppProcess* dvpp = new DvppProcess(); - dvpp->InitResource(stream); - //Base64Utils decoder(); - - - //wh20210623 - std::shared_ptr NV21data[batchSize]; - - if (format != 0 && format != 1) + if (format != 1) { - printf("ERROR FORMAT TYPE: %d\n", format); - return -1; + LOG_ERROR("ERROR FORMAT TYPE: {}", format); + return FAILED; } double t1,t2,t3; @@ -258,48 +247,44 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI jstring imageString1 = (jstring)env->GetObjectField(syImgParam, fid_image_str); const char *image_str = env->GetStringUTFChars(imageString1, JNI_FALSE); string imageString = image_str; - - //printf("format = %d\n", format); - if (format == 0) //url - { - printf("url is not surposed \n"); - } - else - { - - string decode_str; - //decode_str = decoder->Decode(encode_str[b].data(), encode_str[b].size()); - decode_str = Decode(imageString.data(), imageString.size()); - - //debug======================================================== - ImageData src; - uint32_t binFileBufferLen = decode_str.size() + 8;//�ֽ��� - uint8_t* binFileBufferData = new(std::nothrow) uint8_t[binFileBufferLen]; - - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - return FAILED; - } - - memcpy((char *)binFileBufferData, decode_str.data(), binFileBufferLen); - int32_t ch = 0; - ACL_CALL(acldvppJpegGetImageInfo(binFileBufferData, binFileBufferLen, - &(src.width), &(src.height), &ch), SUCCESS, FAILED); - src.data.reset(binFileBufferData, [](uint8_t* p) { delete[](p); }); - src.size = binFileBufferLen; - // printf("vpdr_api_batch:img.w=%d ,img.h=%d \n",src.width,src.height); - - ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data[i], src), SUCCESS, FAILED); - - batch_img[i].w_ = dvpp_data[i].width;//dvpp_data[b].alignWidth; - batch_img[i].h_ = dvpp_data[i].height;//dvpp_data[b].alignHeight; - //batch_img[i].data_ = (uint8_t*)Utils::CopyDataDeviceToLocal(dvpp_data[i].data.get(), dvpp_data[i].size);//dvpp_data[b].data.get(); - batch_img[i].data_ = dvpp_data[i].data.get(); - - } - - env->ReleaseStringUTFChars(imageString1, image_str); + string decode_str; + //decode_str = decoder->Decode(encode_str[b].data(), encode_str[b].size()); + decode_str = Decode(imageString.data(), imageString.size()); + env->ReleaseStringUTFChars(imageString1, image_str); + + //debug======================================================== + ImageData src; + uint32_t binFileBufferLen = decode_str.size() + 8;//�ֽ��� + uint8_t* binFileBufferData = new(std::nothrow) uint8_t[binFileBufferLen]; + + if (binFileBufferData == nullptr) { + LOG_ERROR("malloc binFileBufferData failed"); + return FAILED; + } + + src.data.reset(binFileBufferData, [](uint8_t* p) { delete[](p); }); + + memcpy((char *)binFileBufferData, decode_str.data(), binFileBufferLen); + int32_t ch = 0; + ret = acldvppJpegGetImageInfo(binFileBufferData, binFileBufferLen, &(src.width), &(src.height), &ch); + if (SUCCESS != ret) { + LOG_ERROR("acldvppJpegGetImageInfo failed"); + return FAILED; + } + + src.size = binFileBufferLen; + + ret = dvpp->CvtJpegToYuv420sp(dvpp_data[i], src); + if (SUCCESS != ret) { + LOG_ERROR("CvtJpegToYuv420sp failed"); + return FAILED; + } + + batch_img[i].w_ = dvpp_data[i].width;//dvpp_data[b].alignWidth; + batch_img[i].h_ = dvpp_data[i].height;//dvpp_data[b].alignHeight; + //batch_img[i].data_ = (uint8_t*)Utils::CopyDataDeviceToLocal(dvpp_data[i].data.get(), dvpp_data[i].size);//dvpp_data[b].data.get(); + batch_img[i].data_ = dvpp_data[i].data.get(); } t2 = msecond(); //printf("va jni info:decode time: %.2f\n", (t2 - t1)); @@ -309,8 +294,8 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI std::vector vec_result = village_pic_analysis(vaHandle, batch_img, batchSize); if (vec_result.size() <= 0 || vec_result.size() != batchSize) { - printf("jni info:village_pic_analysis failed."); - return -1; + LOG_ERROR("village_pic_analysis failed."); + return FAILED; } cout << "result size:" << vec_result.size() << endl; @@ -344,7 +329,7 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI jclass cls_StainVplateResultParam = env->FindClass("com/objecteye/pojo/vehicle/StainVplateResultParam"); if (nullptr == cls_StainVplateResultParam) { - cout << "cls_StainVplateResultParam find class error!" << endl; + LOG_ERROR("cls_StainVplateResultParam find class error!"); } jclass cls_MannedResultParam = env->FindClass("com/objecteye/pojo/vehicle/MannedResultParam"); @@ -366,13 +351,13 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI jmethodID mid_VplateResult = env->GetMethodID(cls_VplateResult, "", "(Lcom/objecteye/pojo/common/SyRectParam;F[Lcom/objecteye/pojo/vehicle/VehiclePlateNumParam;FIILcom/objecteye/pojo/vehicle/StainVplateResultParam;)V"); if (nullptr == mid_VplateResult) { - cout << "mid_VplateResult GetMethodID error!" << endl; + LOG_ERROR("mid_VplateResult GetMethodID error!"); } jmethodID mid_VehiclePendantDetResult = env->GetMethodID(cls_VehiclePendantDetResult, "", "([Lcom/objecteye/pojo/vehicle/VehiclePendantDetectInfoParam;I)V"); jmethodID mid_VehicleIllegalDetResult = env->GetMethodID(cls_VehicleIllegalDetResult, "", "(Lcom/objecteye/pojo/vehicle/VehicleIllegalDetectInfoParam;Lcom/objecteye/pojo/vehicle/VehicleIllegalDetectInfoParam;)V"); if (nullptr == mid_VehicleIllegalDetResult) { - cout << "mid_VehicleIllegalDetResult GetMethodID error!" << endl; + LOG_ERROR("mid_VehicleIllegalDetResult GetMethodID error!"); } jmethodID mid_VehicleFeaResult = env->GetMethodID(cls_VehicleFeaResult, "", "([F)V"); jmethodID mid_SyRect = env->GetMethodID(cls_SyRect, "", "(IIII)V"); @@ -618,12 +603,6 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI env->SetObjectArrayElement(vehicleAnalysisResultParams, i, vehicleAnalysisResult); } - ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, FAILED); - delete dvpp; - dvpp = nullptr; - (void) aclrtDestroyStream(stream); - aclrtDestroyContext(ctx); - return SUCCESS; } @@ -639,6 +618,23 @@ JNIEXPORT void JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI { village_pic_release(&vaHandle); } + + if (ctx) { + aclrtSetCurrentContext(ctx); + } + + if (dvpp) { + delete dvpp; + dvpp = nullptr; + } + + if (stream) { + (void) aclrtDestroyStream(stream); + } + + if (ctx) { + aclrtDestroyContext(ctx); + } } /* -- libgit2 0.21.4