gpt4 book ai didi

c++ - 我怎样才能堵住这个 C++ 内存泄漏?

转载 作者:行者123 更新时间:2023-11-30 05:39:55 25 4
gpt4 key购买 nike

我有一个程序给出了预期的输出,但是我在我的输出下面收到了以下错误"

* glibc detected * PathFinder2: free(): invalid pointer: 0xb6046b70 ***

我使用 Valgrind 运行我的程序。但是我没有名为 pthread_create.cpthread_create.c 的文件。

我如何根据 Valgrind 的这些统计信息解决错误:

==3439== 800 bytes in 1 blocks are definitely lost in loss record 100 of 103

==3439== at 0x402ADFC: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)

==3439== by 0x80560B5: Assignm3::Maze::InitMazeArray() (in /home/a/Desktop/PathFinder2)

==3439== by 0x8055167: Assignm3::Maze::LoadMaze(std::string) (in /home/a/Desktop/PathFinder2)

==3439== by 0x804A1F8: solveMaze(void*) (in /home/a/Desktop/PathFinder2)

==3439== by 0x4052F6F: start_thread (pthread_create.c:312)

==3439== by 0x42A170D: clone (clone.S:129)

更新这部分是否存在逻辑错误导致内存泄漏。我似乎找不到任何错误。

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;
}

最佳答案

似乎您在 Assignm3::Maze::InitMazeArray() 中有 new[] 而没有 delete[]。

关于c++ - 我怎样才能堵住这个 C++ 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com