我刚开始学习C++,并面临一个`free():invalid pointer:error。在GDB调试,错误显示C++指针内存泄漏
[主题0xb7caab40(LWP 4405)退出]
*错误的`/家庭/ A /桌面/ PathFinder2' :免费():无效的指针:0xb74a9b40 *
编程接收信号SIGABRT,中止。 0xb7fdd424在 __kernel_vsyscall()
在valgrid
== 4487 ==在1块800个字节肯定在损失记录1
== 4487 == 1在0x402ADFC丢失:operator new [](unsigned int)(in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
== 4487 == by 0x80560B9:Assignm3 :: Maze :: InitMazeArray()(in/home/a/Desktop/PathFinder2)
== == 4487通过0x805516B:Assignm3 ::迷宫:: LoadMaze(的std :: string)(在/ home/A /桌面/ PathFinder2)
== == 4487通过0x804A3C4:solveMaze(无效*)(在/ home /一个/桌面/ PathFinder2)
== 4487 ==由0x4052F6F:start_thread(pthread_create.c:312)
== 4487 ==由0x42A170D:克隆(clone.S :129)
== 4487 ==
== 4487 == LEAK SUMMARY:
== 4487 ==绝对丢失:800个字节在1块
== 4487 ==间接丢失:0字节0块
== 4487 ==可能丢失:在0 0字节块
== 4487 ==仍可达:在0块
== 4487 ==抑制0字节:在0块
== 4487 ==
字节== 4487 ==错误摘要:
== 4487 ==
== 4487 ==在2上下文1 1个错误:
:从2个上下文(0 0抑制)2个错误== 4487 ==无效免费()/删除/删除[]/realloc()的
== 4487 ==在0x402B838:操作者删除(无效*)(在/ usr/LIB /的valgrind/vgpreload_memcheck -x86-Linux操作系统。所以)
== == 4487通过0x804EF2E:__gnu_cxx :: new_allocator :: DEALLOCATE(点*,无符号整型)(在/ home/A /桌面/ PathFinder2)
== 4487通过0x804E9F6 == :性病:: _ Vector_base :: _ M_deallocate(点*,无符号整型)(在 /家庭/ A /桌面/ PathFinder2)
== == 4487通过0x804E883:性病:: _ Vector_base> ::〜_Vector_base( )( /home/a/Desktop/PathFinder2)
== 4487 == by 0x804E2F6:std :: vector> ::〜vector()(在/ home/a/Desktop/PathFinder2)
== == 4487通过0x804E25B:Assignm3 :: PathFinderResource ::〜PathFinderResource()(在 /家庭/ A /桌面/ PathFinder2) == == 4487通过0x41E83D0:__run_exit_handlers(exit.c中:82 )
== 4487 ==由0x41E842C:出口(exit.c中:104)
== 4487 ==由0x41CEA8A:(下面主要)(的libc-start.c:321)
== 4487 ==地址0x5768b40不堆栈,malloc'd或(近期)释放
== 448 7 ==
== 4487 ==错误摘要:从2个上下文(抑制:0 0)2个错误
这是我的编码
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();
}
};
'我刚开始学习C++'你为什么要跳进使用线程,如果你刚开始学习C++?没有这个错误的一种方法是使用智能指针,而不是原始指针。 – PaulMcKenzie
请减少代码,以便我们更容易地消化问题。很可能你正在删除一个已经被删除或者没有被分配的指针,或者你已经被分配了大小的指针 – cppguy
PaulMcKenzie - 我的任务需要线程化。 cppguy-我如何知道是否即时删除已被删除的指针?我试图评论我的主要功能不释放内存,错误仍然存在。谢谢 – Happy