인프런 커뮤니티 질문&답변

bong님의 프로필 이미지
bong

작성한 질문수

[C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part3: 유니티 엔진

5강 Property 내부에서 Init() 호출하면 안되는 이유가 궁금합니다.

해결된 질문

작성

·

126

0

5강 마지막에 보면 인터페이스 최적화를 위해 이렇게 구현을 하셨는데요...

public static Managers Instance { get { Init(); return s_instance; } } // 유일한 매니저를 갖고 온다

아래처럼 에러가 나고 있습니다. 마치 무한히 재귀호출이 일어나는 것처럼 보이는데... 그래서 Init(); 호출을 지우면 이런 오류는 안나옵니다. 무한 호출이 일어나는 이유를 알 수 있을까요?

StackOverflowException: The requested operation caused a stack overflow. Managers.get_Instance () <0x252b47f87f0 + 0x00008> in <3152d6b1a9334587bbe43f1d8528b776>:0 Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.Init () (at Assets/Scripts/Managers/Managers.cs:25) Managers.get_Instance () (at Assets/Scripts/Managers/Managers.cs:8) Managers.In<message truncated>

답변 2

0

bong님의 프로필 이미지
bong
질문자

아, 확인해보니 Init() 안쪽 조건문에서 Instance 변수를 그대로 놔뒀었습니다..ㅠ s_instance로 고치니 문제 안 생기네요. 감사합니다!

0

Rookiss님의 프로필 이미지
Rookiss
지식공유자

위 내용만으로는 알 수 없고
Init() 코드 내부가 관건입니다.

public static Managers Instance { get { Init(); return s_instance; } }

작성하신 코드에서 Instance 내부에서 Init()을 호출하는데,
Init() 내부에서는 다시 Instance를 사용할 것으로 예상됩니다.
Init -> Instance -> Init -> Instance -> ...
이렇게 무한으로 호출하다 보니
함수 호출을 위한 스택 메모리가 고갈되어 발생하는 것입니다.
따라서 Init 내부에서는 Instance를 사용하면 안되고, s_instance를 사용해야
해당 문제를 우회할 수 있습니다.

bong님의 프로필 이미지
bong

작성한 질문수

질문하기