C++指针内存泄漏
C++ pointer memory leak
我刚开始学习c++,我正面临一个`free((:无效指针:错误。在gdb调试下,错误显示
[线程0xb7caab40(LWP 4405(退出]
*"/home/a/Desktop/PathFinder2"中出错:free((:无效指针:0xb74a9b40*
程序收到信号SIGABRT,中止。0xb7fdd424英寸__kernel_vsyscall((
valgrid
==4487==1个块中的800字节在1个的丢失记录1中肯定丢失
==4487==在0x402ADFC:operator new[](unsigned int((在/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so中(
===4487===通过0x80560B9:Assignm3::Maze::InitMazeArray(((在/home/a/Desktop/PathFinder2中(
==4487==by 0x805516B:Assignm3::Maze::LoadMaze(std::string((在/home/a/Desktop/PathFinder2中(
===4487===通过0x804A3C4:solveMaze(void*((在/home/a/Desktop/PathFinder2中(
===4487===通过0x4052F6F:start_thread(pthread_create.c:312(
===4487===通过0x42A170D:克隆(克隆.S:129(
===4487===
===4487===泄漏摘要:
===4487===肯定丢失:1块中有800个字节
===4487===间接丢失:0块中有0个字节
===4487===可能丢失:0块中有0个字节
===4487===仍然可访问:0块中有0个字节
===4487===已抑制:0个块中的0个字节
===4487===
===4487===错误摘要:2个上下文中的2个错误(已抑制:0中的0(
===4487===
===4487===上下文1中的1个错误,共2个:
===4487===free((/delete/delete[]/realloc((无效
==4487==在0x402B838处:操作员删除(void*((在/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so中(
===4487===通过0x804EF2E:__gnu_cxx::new_allocator::deallocate(Point*,unsigned int((在/home/a/Desktop/PathFinder2中(
===4487===通过0x804E9F6:std::_Vector_base::_M_deallocate(点*,无符号int((in/home/a/Desktop/PathFinder2(
==4487==通过0x804E883:std::_Vector_base>::~ _Vector_base(((在/home/a/Desktop/PathFinder2(
===4487===通过0x804E2F6:std::vector>::~vector(((在/home/a/Desktop/PathFinder2中(
===4487===通过0x804E25B:Assignm3::PathFinderResource::~PathFinderResource(((在/home/a/Desktop/PathFinder2(===4487===由0x41E83D0:__run_exit_handlers(exit.c:82(
===4487===通过0x41E842C:退出(退出.c:104(
===4487===通过0x41CEA8A:(主目录下((libc启动.c:321(
===4487===地址0x5768b40不是堆栈的、malloc的或(最近(空闲的
===4487===
===4487===错误摘要:2个上下文中的2个错误(已抑制:0中的0(
这是我的编码
static void *solveMaze(void *vptr_args)
{
Point point1, point2, point3;
int nxtPx, nxtPy;
mazeObj->LoadMaze();
point1 = mazeObj->getStartLocation();
point3 = mazeObj->getEndLocation();
VectorOfPointStructType Path, vecMain;
Path.push_back(point1);
vecMain.push_back(point1);
point2 = mazeObj->getEndLocation();
nxtPx = point1.getX();
nxtPy = point1.getY();
sleep(3);
while (true)
{
string thread1, thread2;
thread1 = THREAD_NAMES[line1];
thread2 = THREAD_NAMES[line2];
cout << "Thread " << THREAD_NAMES[line1] << " is running" << endl;
cout << "Thread " << THREAD_NAMES[line2] << " is running" << endl;
for (int x = 0; x < 5; x++)
{
if (thread1 != THREAD_NAMES[line1] && thread2 != THREAD_NAMES[line2])
{
cout << "Thread " << THREAD_NAMES[line1] << " is running" << endl;
cout << "Thread " << THREAD_NAMES[line2] << " is running" << endl;
}
if (x == 0)
{
nxtPx++;
point3 = Point(nxtPx, nxtPy);
if ((mazeObj->IsThereBarrier(point3) || mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3, ob))
{
Path.push_back(point3);
vecMain.push_back(point3);
ob.push_back(point3);
if (mazeObj->IsThereBarrier(point3))
{
pthread_mutex_lock(&mutex1);
if (submitMazeSolnObj->submitPathToBarrier(pthread_self(), Path))
{
sleep(1);
}
pthread_mutex_unlock(&mutex1);
}
else if (mazeObj->IsThereDanger(point3))
{
pthread_mutex_lock(&mutex1);
if (submitMazeSolnObj->submitPathToDangerArea(pthread_self(), Path))
{
if (thread1 == THREAD_NAMES[line1])
{
diemsg1();
}
else if (thread2 == THREAD_NAMES[line2])
{
diemsg2();
}
create();
}
pthread_mutex_unlock(&mutex1);
}
nxtPx--;
Path.pop_back();
}
else if ((!mazeObj->IsThereBarrier(point3) && !mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3,
vecMain))
{
point1 = point3;
Path.push_back(point1);
vecMain.push_back(point1);
point3 = mazeObj->getEndLocation();
x = - 1;
if (point1.isConnected(point2) || reachEnd)
{
reachEnd = true;
break;
}
}
else
{
nxtPx--;
}
}
else if (x == 1)
{
nxtPy++;
point3 = Point(nxtPx, nxtPy);
if ((mazeObj->IsThereBarrier(point3) || mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3, ob))
{
Path.push_back(point3);
vecMain.push_back(point3);
ob.push_back(point3);
if (mazeObj->IsThereBarrier(point3))
{
pthread_mutex_lock(&mutex1);
if (submitMazeSolnObj->submitPathToBarrier(pthread_self(), Path))
{
sleep(1);
}
pthread_mutex_unlock(&mutex1);
}
else if (mazeObj->IsThereDanger(point3))
{
pthread_mutex_lock(&mutex1);
if (submitMazeSolnObj->submitPathToDangerArea(pthread_self(), Path))
{
if (thread1 == THREAD_NAMES[line1])
{
diemsg1();
}
else if (thread2 == THREAD_NAMES[line2])
{
diemsg2();
}
create();
}
pthread_mutex_unlock(&mutex1);
}
nxtPy--;
Path.pop_back();
}
else if ((!mazeObj->IsThereBarrier(point3) && !mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3,
vecMain))
{
point1 = point3;
Path.push_back(point1);
vecMain.push_back(point1);
point3 = mazeObj->getEndLocation();
x = - 1;
if (point1.isConnected(point2) || reachEnd)
{
reachEnd = true;
break;
}
}
else
{
nxtPy--;
}
}
else if (x == 2)
{
nxtPx--;
point3 = Point(nxtPx, nxtPy);
if ((mazeObj->IsThereBarrier(point3) || mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3, ob))
{
Path.push_back(point3);
vecMain.push_back(point3);
ob.push_back(point3);
if (mazeObj->IsThereBarrier(point3))
{
pthread_mutex_lock(&mutex1);
if (submitMazeSolnObj->submitPathToBarrier(pthread_self(), Path))
{
sleep(2);
}
pthread_mutex_unlock(&mutex1);
}
else if (mazeObj->IsThereDanger(point3))
{
pthread_mutex_lock(&mutex1);
if (submitMazeSolnObj->submitPathToDangerArea(pthread_self(), Path))
{
if (thread1 == THREAD_NAMES[line1])
{
diemsg1();
}
else if (thread2 == THREAD_NAMES[line2])
{
diemsg2();
}
create();
}
pthread_mutex_unlock(&mutex1);
}
nxtPx++;
Path.pop_back();
}
else if ((!mazeObj->IsThereBarrier(point3) && !mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3,
vecMain))
{
point1 = point3;
Path.push_back(point1);
vecMain.push_back(point1);
point3 = mazeObj->getEndLocation();
x = - 1;
if (point1.isConnected(point2) || reachEnd)
{
reachEnd = true;
break;
}
}
else
{
nxtPx++;
}
}
else if (x == 3)
{
nxtPy--;
point3 = Point(nxtPx, nxtPy);
if ((mazeObj->IsThereBarrier(point3) || mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3, ob))
{
Path.push_back(point3);
vecMain.push_back(point3);
ob.push_back(point3);
if (mazeObj->IsThereBarrier(point3))
{
pthread_mutex_lock(&mutex1);
if (submitMazeSolnObj->submitPathToBarrier(pthread_self(), Path))
{
sleep(2);
}
pthread_mutex_unlock(&mutex1);
}
else if (mazeObj->IsThereDanger(point3))
{
pthread_mutex_lock(&mutex1);
if (submitMazeSolnObj->submitPathToDangerArea(pthread_self(), Path))
{
if (thread1 == THREAD_NAMES[line1])
{
diemsg1();
}
else if (thread2 == THREAD_NAMES[line2])
{
diemsg2();
}
create();
}
pthread_mutex_unlock(&mutex1);
}
nxtPy++;
Path.pop_back();
}
else if ((!mazeObj->IsThereBarrier(point3) && !mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3,
vecMain))
{
point1 = point3;
Path.push_back(point1);
vecMain.push_back(point1);
point3 = mazeObj->getEndLocation();
x = - 1;
if (point1.isConnected(point2) || reachEnd)
{
reachEnd = true;
break;
}
}
else
{
nxtPy++;
}
}
else if (x == 4)
{
pthread_mutex_lock(&mutex1);
for (;;)
{
point1 = Path[Path.size() - 2];
nxtPx = point1.getX();
nxtPy = point1.getY();
point3 = Point(nxtPx - 1, nxtPy);
if (!pathObj->isLocationInPath(point3, vecMain))
{
x = 0;
Path.pop_back();
if (mazeObj->IsThereBarrier(point3))
{
point3 = mazeObj->getEndLocation();
}
pthread_mutex_unlock(&mutex1);
break;
}
point3 = Point(nxtPx, nxtPy + 1);
if (!pathObj->isLocationInPath(point3, vecMain))
{
x = - 1;
Path.pop_back();
if (mazeObj->IsThereBarrier(point3))
{
point3 = mazeObj->getEndLocation();
}
pthread_mutex_unlock(&mutex1);
break;
}
point3 = Point(nxtPx + 1, nxtPy);
if (!pathObj->isLocationInPath(point3, vecMain))
{
x = 2;
Path.pop_back();
if (mazeObj->IsThereBarrier(point3))
{
point3 = mazeObj->getEndLocation();
}
pthread_mutex_unlock(&mutex1);
break;
}
point3 = Point(nxtPx, nxtPy - 1);
if (!pathObj->isLocationInPath(point3, vecMain))
{
x = 1;
Path.pop_back();
if (mazeObj->IsThereBarrier(point3))
{
point3 = mazeObj->getEndLocation();
}
pthread_mutex_unlock(&mutex1);
break;
}
Path.pop_back();
}
pthread_mutex_unlock(&mutex1);
int deX = point1.getX();
int deY = point1.getY();
if (thread1 == THREAD_NAMES[line1])
{
time(&end);
cout << "Thread: " << THREAD_NAMES[line1] << " found the dead end at " << "[ " << deX << " , " << deY << " ]" << endl;
cout << "Time Elapsed: " << difftime(end, start) << " seconds" << endl;
}
else if (thread2 == THREAD_NAMES[line2])
{
time(&end);
cout << "Thread: " << THREAD_NAMES[line2] << " found the dead end at " << "[ " << deX << " , " << deY << " ]" << endl;
cout << "Time Elapsed: " << difftime(end, start) << " seconds" << endl;
}
}
else
{}
}
{
break;
}
}
Path.push_back(point2);
if (!submitPath)
{
submitMazeSolnObj->submitSolutionPath(pthread_self(), Path);
submitPath = true;
}
return NULL;
}
区块报价
int main()
{
AllocateProgramsVariableMemory();
mazeObj->LoadMaze();
string thread_Msg = "";
string * thread_message = &thread_Msg;
if (pthread_create(&myPFR.activeThreadArray[line1], NULL, solveMaze, thread_message) != 0)
{
return EXIT_FAILURE;
}
else
{
cout << THREAD_NAMES[line1] << " has been created" << endl;
}
//---------------causing error--------------------------------------------
if (pthread_create(&myPFR.activeThreadArray[line2], NULL, solveMaze, thread_message) != 0)
{
return EXIT_FAILURE;
}
else
{
cout << THREAD_NAMES[line2] << " has been created" << endl;
}
//------------------------------------------------------------------------
if (pthread_join(myPFR.activeThreadArray[line1], NULL) != 0)
{
return EXIT_FAILURE;
}
if (pthread_join(myPFR.activeThreadArray[line2], NULL) != 0)
{
return EXIT_FAILURE;
}
DeallocateProgramsVariableMemory();
return EXIT_SUCCESS;
}
区块报价
typedef std::vector<Point> VectorOfPointStructType;
namespace myx
static Maze * mazeObj;
static Path * pathObj;
static SubmitMazeSoln * submitMazeSolnObj;
static void AllocateProgramsVariableMemory (void)
{
mazeObj = new Maze ();
pathObj = new Path ();
submitMazeSolnObj = new SubmitMazeSoln ();
logFileStream.open (DefaultLogFilename.c_str(), std::fstream::out);
}
static void DeallocateProgramsVariableMemory (void)
{
delete mazeObj;
delete pathObj;
delete submitMazeSolnObj;
logFileStream.close ();
pthread_mutex_destroy ( &thread_mutex );
pthread_cond_destroy ( &thread_condition );
}
struct PathFinderResource
{
pthread_t activeThreadArray [MAX_NO_OF_THREADS];
PathFinderParameterInfo * activeThreadParamArray [MAX_NO_OF_THREADS];
VectorOfPointStructType solutionPath;
VectorOfPointStructType discoveredDangerAreas;
int usedThreadNameIndex;
int noOfDeadEndPathsFound;
int noOfBarriersDiscovered;
int noOfDangerAreaDiscovered;
PathFinderResource (void)
{
usedThreadNameIndex = 0;
noOfDeadEndPathsFound = 0;
noOfBarriersDiscovered = 0;
noOfDangerAreaDiscovered = 0;
solutionPath = VectorOfPointStructType ();
discoveredDangerAreas = VectorOfPointStructType ();
}
~PathFinderResource (void)
{
solutionPath.clear ();
discoveredDangerAreas.clear ();
}
};
static void *solveMaze(void *vptr_args)
{
//...
mazeObj->LoadMaze();
//...
}
int main()
{
static Maze * mazeObj;
static Path * pathObj;
static SubmitMazeSoln * submitMazeSolnObj;
//...
}
参考上面的代码。。。main中的mazeObj不可能在solveMaze中具有作用域。solveMaze从哪里得到它的mazeObj。要么它不是同一个mazeObj,要么这不是真正的代码。
问候,
Werner
- C++功能泄漏内存,我是C++新手,不确定如何解决
- 我的堆栈弹出式磁带的实现是否泄漏内存?
- 将 c++ 向量转换为字符 ** 而不会泄漏内存
- 析构函数 C++ 使泄漏内存
- 构造函数对象赋值是否泄漏内存
- corba :: orb_init泄漏内存
- Gmock泄漏内存
- 如何在不泄漏内存的情况下删除链接列表
- Visual C ODBC关闭记录集泄漏内存
- 为什么泄漏内存比在动态数组上执行 delete[] 慢
- OpenGL正在泄漏内存.哪个对象未释放
- 可以std ::退出泄漏内存
- uiautomation findall泄漏内存
- 为什么在此OpenCL代码中泄漏内存,为什么要泄漏内存
- pthread在完成后会泄漏内存
- win32 标准::线程泄漏内存
- 返回指向同一变量的指针是否会泄漏内存
- 使用clectType(new any_type())可能会泄漏内存泄漏
- Windows开发:如何确定我的应用程序是否正在泄漏内存
- WinHttp打开泄漏内存