C++调用函数莫名错误,用debug模式程序正常,release模式程序卡在一个函数中,如果不调用这个函数,程序正常结束,但调用这个函数就内存错误,就算函数为空也会这样。会是什么原因呢。用到了opencv库
存在数组越界或者野指针之类的错误,因为debug模式,内存使用比较宽松,当你越界一点,并不破坏别的变量,导致问题比较隐蔽,没有暴露。
不知道你这个问题是否已经解决, 如果还没有解决的话:Debug.h:
#ifndef DETECT_DEBUG_H
#define DETECT_DEBUG_H
#include "opencv2/opencv.hpp"
#include <chrono>
#define millisecond 1000000
using namespace std;
//debug info ON-OFF
#define __DEBUG__ON
#ifdef __DEBUG__ON
#define __DEBUG__WIN__ON //Window debug:print debug info
#define __DEBUG__IMSHOW__ON //show debug images
#define __DEBUG__IMWRITE__OFF //write debug images
#define __DEBUG__TIME__ON //run times test on/off
#define __DEBUG__ANDROID__OFF //android debug on/off
//#include <assert.h>
//#define DEBUG_ASSERT(...) assert( __VA_ARGS__)
//#define DEBUG_CV_ASSERT(...) CV_Assert( __VA_ARGS__)
#else
#define __DEBUG__ON(format,...)
#endif
//print debug info
#ifdef __DEBUG__WIN__ON
//#define DEBUG_PRINT(...) printf("File: %s, Line: %05d: "format"", __FILE__,__LINE__, ##__VA_ARGS__)
#define DEBUG_PRINT(...) printf( __VA_ARGS__);printf("\n")
#else
#define DEBUG_PRINT(format,...)
#endif
//show debug images
#ifdef __DEBUG__IMSHOW__ON
#define DEBUG_IMSHOW(...) showImages(__VA_ARGS__)
#else
#define DEBUG_IMSHOW(format,...)
#endif
//write debug images
#ifdef __DEBUG__IMWRITE__ON
#define DEBUG_IMWRITE(...) saveImage(__VA_ARGS__)
#else
#define DEBUG_IMWRITE(format,...)
#endif
//write debug images
#ifdef __DEBUG__ANDROID__ON
#include <android/log.h>
// Define the LOGI and others for print debug infomation like the log.i in java
#define LOG_TAG "SmartAlbum -- JNILOG"
//#undef LOG
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG, __VA_ARGS__)
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,LOG_TAG, __VA_ARGS__)
#else
#ifdef __DEBUG__WIN__ON
#define LOGI(...) printf( __VA_ARGS__); printf("\n")
#define LOGD(...) printf( __VA_ARGS__); printf("\n")
#define LOGW(...) printf( __VA_ARGS__); printf("\n")
#define LOGE(...) printf( __VA_ARGS__); printf("\n")
#define LOGF(...) printf( __VA_ARGS__); printf("\n")
#else
#define LOGI(...)
#define LOGD(...)
#define LOGW(...)
#define LOGE(...)
#define LOGF(...)
#endif
#endif
//run times test...
#ifdef __DEBUG__TIME__ON
#define LOG_TIME LOGE
#define RUN_TIME(time_) (double)(time_).count()/millisecond
//#define RUN_TIME(...) getTime_MS( __VA_ARGS__)
//设置计算运行时间的宏定义
#define DEBUG_TIME(time_) auto time_ =std::chrono::high_resolution_clock::now()
#define DEBUG_TIME_PRINT(time_) printf("run time: %s=%3.3f ms\n", #time_,(double)(time_).count()/millisecond)
#else
#define DEBUG_TIME(time_)
#endif
template<typename TYPE>
void PRINT_1D(string name,TYPE *p1, int len) {
printf("%s", name.c_str());
for (int i = 0; i < len; i++) {
printf("%f,", p1[i]);
}
cout << endl;
}
void showImages(const char *imageName, cv::Mat image);
void showImages(const char *imageName, cv::Mat img, cv::Rect face);
void showImages(const char *imageName, cv::Mat img, cv::Rect face, std::vector<cv::Point> pts);
void showImages(const char *imageName, cv::Mat img, std::vector<cv::Point> pts);
void saveImage(const char *imageName, cv::Mat image);
void saveImage(const char *imageName, cv::Mat image, std::vector<int> para);
void saveImage(const char *imageName, cv::Mat image, cv::Rect face, std::vector<cv::Point> pts);
void saveImage(const char *imageName, cv::Mat img, cv::Rect face);
vector<string> getFilesList(string dir);
void writeDatatxt(string path, string data, bool bCover=false);
#ifdef linux
#define _LINUX
#define separator "/"
#endif
#ifdef _WIN32//__WINDOWS_
#define _WINDOWS
#define separator "\\"
#endif
#endif
Debug.cpp:
#include "opencv2/opencv.hpp"
using namespace std;
#include "debug.h"
//*************************************显示图片****************************************
#define RESIZE(img_show,col) cv::resize(img_show, img_show, cv::Size(col, img_show.rows*col / img_show.cols))
void showImages(const char *imageName, cv::Mat img) {
cv::Mat img_show = img.clone();
if (img_show.channels() == 1)
cvtColor(img_show, img_show, cv::COLOR_GRAY2BGR);
//char str[200];
char str1[200];
strcpy(str1, imageName);
//sprintf(str, ",Size:%dx%d", image.rows, image.cols);
//strcat(str1, str);
RESIZE(img_show, 400);
cv::imshow(str1, img_show);
cv::waitKey(100);
}
void showImages(const char *imageName, cv::Mat img, cv::Rect face, std::vector<cv::Point> pts)
{
cv::Mat img_show = img.clone();
if (img_show.channels() == 1)
cvtColor(img_show, img_show, cv::COLOR_GRAY2BGR);
for (int i = 0; i < pts.size(); ++i) {
//std::cout << "index: " << i << std::endl;
cv::circle(img_show, pts.at(i), 2.f, cv::Scalar(0, 0, 255), -1, CV_AA);
// char str[3];
// itoa(i, str, 10);
// //line(imgDrawFace, cv::Point(shape.part(i).x(), shape.part(i).y()), cv::Point(shape.part(i).x(), shape.part(i).y()), cv::Scalar(0, i * 3, 255), 2);
// putText(img_show, str, pts.at(i), cv::FONT_HERSHEY_DUPLEX, 0.5, cv::Scalar(255, 0, 0));
}
cv::rectangle(img_show, face, { 255, 0, 0 }, 2);
char str[200];
char str1[200];
strcpy(str1, imageName);
//sprintf(str, ",Size:%dx%d", img.rows, img.cols);
//strcat(str1, str);
RESIZE(img_show, 400);
cv::imshow(str1, img_show);
cv::waitKey(100);
}
void showImages(const char *imageName, cv::Mat img, std::vector<cv::Point> pts)
{
cv::Mat img_show = img.clone();
if (img_show.channels() == 1)
cvtColor(img_show, img_show, cv::COLOR_GRAY2BGR);
for (int i = 0; i < pts.size(); ++i) {
//std::cout << "index: " << i << std::endl;
cv::circle(img_show, pts.at(i), 2.f, cv::Scalar(0, 0, 255), -1, CV_AA);
// char str[3];
// itoa(i, str, 10);
// //line(imgDrawFace, cv::Point(shape.part(i).x(), shape.part(i).y()), cv::Point(shape.part(i).x(), shape.part(i).y()), cv::Scalar(0, i * 3, 255), 2);
// putText(img_show, str, pts.at(i), cv::FONT_HERSHEY_DUPLEX, 0.5, cv::Scalar(255, 0, 0));
}
char str[200];
char str1[200];
strcpy(str1, imageName);
//sprintf(str, ",Size:%dx%d", img.rows, img.cols);
//strcat(str1, str);
RESIZE(img_show, 400);
cv::imshow(str1, img_show);
cv::waitKey(100);
}
void showImages(const char *imageName, cv::Mat img, cv::Rect face)
{
int thickness = img.cols*0.005;
thickness = thickness > 1 ? thickness : 1;
cv::Mat img_show = img.clone();
if (img_show.channels() == 1)
cvtColor(img_show, img_show, cv::COLOR_GRAY2BGR);
cv::rectangle(img_show, face, { 255, 0, 0 }, thickness);
char str[200];
char str1[200];
strcpy(str1, imageName);
//sprintf(str, ",Size:%dx%d", img.rows, img.cols);
//strcat(str1, str);
RESIZE(img_show, 400);
cv::imshow(str1, img_show);
cv::waitKey(100);
}
//*************************************保存图片****************************************
void saveImage(const char *imageName, cv::Mat image) {
cv::imwrite(imageName, image);
}
void saveImage(const char *imageName, cv::Mat image, std::vector<int> para) {
cv::imwrite(imageName, image, para);
}
void saveImage(const char *imageName, cv::Mat image, cv::Rect face, std::vector<cv::Point> pts) {
int thickness = image.cols*0.005;
thickness = thickness > 1 ? thickness : 1;
cv::Mat img = image.clone();
for (int i = 0; i < pts.size(); ++i) {
//std::cout << "index: " << i << std::endl;
cv::circle(img, pts.at(i), 2.f, cv::Scalar(0, 0, 255), thickness, CV_AA);
}
cv::rectangle(img, face, { 255, 0, 0 }, thickness);
cv::imwrite(imageName, img);
}
void saveImage(const char *imageName, cv::Mat img, cv::Rect face)
{
cv::Mat img_show = img.clone();
if (img_show.channels() == 1)
cvtColor(img_show, img_show, cv::COLOR_GRAY2BGR);
cv::rectangle(img_show, face, { 255, 0, 0 }, 2);
cv::imwrite(imageName, img_show);
}
//*************************************获取文件列表****************************************
#ifdef _LINUX
#include <memory.h>
#include <dirent.h>
vector<string> getFilesList(string dirpath) {
vector<string> allPath;
DIR *dir = opendir(dirpath.c_str());
if (dir == NULL)
{
cout << "opendir error" << endl;
return allPath;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_DIR) {//It's dir
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
string dirNew = dirpath + separator + entry->d_name;
vector<string> tempPath = getFilesList(dirNew);
allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
}
else {
//cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;
string name = entry->d_name;
string imgdir = dirpath + separator + name;
//sprintf("%s",imgdir.c_str());
allPath.push_back(imgdir);
}
}
closedir(dir);
//system("pause");
return allPath;
}
#endif
#ifdef _WIN32//__WINDOWS_
#include <io.h>
vector<string> getFilesList(string dir)
{
vector<string> allPath;
// 在目录后面加上"\\*.*"进行第一次搜索
string dir2 = dir + separator+"*.*";
intptr_t handle;
_finddata_t findData;
handle = _findfirst(dir2.c_str(), &findData);
if (handle == -1) {// 检查是否成功
cout << "can not found the file ... " << endl;
return allPath;
}
while (_findnext(handle, &findData) == 0)
{
if (findData.attrib & _A_SUBDIR)//// 是否含有子目录
{
//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索
if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
continue;
// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
string dirNew = dir + separator + findData.name;
vector<string> tempPath = getFilesList(dirNew);
allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
}
else //不是子目录,即是文件,则输出文件名和文件的大小
{
string filePath = dir + separator + findData.name;
allPath.push_back(filePath);
}
}
_findclose(handle); // 关闭搜索句柄
return allPath;
}
#endif
//***********************************将数据保存到txt文本中*************************************
void writeDatatxt(string path, string data, bool bCover) {
//fstream fout(path, ios::app);
fstream fout;
if (bCover)
{
fout.open(path);//默认是:ios_base::in | ios_base::out
}
else
{
fout.open(path, ios::app);//所有写入附加在文件末尾
}
fout << data << endl;
fout.flush();
fout.close();
}
破案,函数没有写返回值,我去,这个错误第一次遇到好诡异呀。