我剛開始學習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