2014-02-25 2 views
0

좋아요. 그래서 플레이어의 벡터에 추가 할 수있는 스레드가 있습니다. 그러나 push_back 함수를 호출 할 때마다 메모리 액세스 위반이 발생하며 벡터가있는 다른 모든 코드를 꺼냅니다. 이 스레드 외부에서 사용되고 있습니다.참조 된 벡터에 push_back을 할 수 없습니다

이 문제가 발생하기 전에 벡터 크기를 읽을 수 있지만 push_back을 사용할 수 없습니다.

벡터는 다음과 같습니다

std::vector<A_Player> &clientsRef; 

ADN가에있는 스레드는 다음과 같습니다

void NetworkManager::TCPAcceptClient(){ 
std::cout << "Waiting to accept that client that pinged us" << std::endl; 
fd_set fd; 
timeval tv; 
FD_ZERO(&fd); 
FD_SET(TCPListenSocket, &fd); 

tv.tv_sec = 5;//seconds 
tv.tv_usec = 0;//miliseconds 

A_Player thePlayer; 
thePlayer.sock = SOCKET_ERROR; 

if (select(0, &fd, NULL, NULL, &tv) > 0){ //using select to allow a timeout if the client fails to connect 
    if (thePlayer.sock == SOCKET_ERROR){ 
     thePlayer.sock = accept(TCPListenSocket, NULL, NULL); 
    } 

    thePlayer.playerNumber = clientsRef.size() + 1; 
    thePlayer.isJumping = false; 
    thePlayer.X = 0; 
    thePlayer.Y = 0; 
    thePlayer.Z = 0; 
    clientsRef.push_back(thePlayer); 

    clientHandler = std::thread(&NetworkManager::ClientRecieve, this); 
    clientHandler.detach(); 
} 
else{ 
    std::cout << "Client connection timed out!!!!!" << std::endl; 
} 

}

누구도 날이하지 않는 이유에 대한 통찰력을 줄 수 작업?

종류는

+1

이 참조는 어떻게 초기화됩니까 (표준 : 벡터 & clientsRef는 참조이며 초기화되어야합니다)? –

+0

을 통해 생성자 –

+1

아니요, 참조에는 생성자가 없습니다. '&'기호를보십시오. clientRef가 초기화되지 않았기 때문에 clientsRef는 참조를 만들고 컴파일러는'std :: vector & clientsRef;'와 같은 라인에 대해 불평해야합니다. –

답변

0

내가 참조한 것은 내가 가지고있는 문제에 대해 작동하지 않을 것이고 나는 잘 작동 한 포인터 시스템으로 변환했다.

1

내 심령 디버깅 기술이 clientsRef 참조가 파괴 현지 벡터를 참조하고 있음을 말해 간주한다. 참조를 설정 한 코드를 살펴보십시오.

관련 문제