2017-10-26 13 views
1

내 앱 카메라 미리보기 레코드 앱. 기록 카메라 미리보기 중에 ArrayList을 사용합니다. WeakReference를 사용하면 android에서 심볼 메시지를 해결할 수 없습니다.

ArrayList

는 전역 변수

private ArrayList<OutputInputPair> pairs = new ArrayList<OutputInput>();

선언 나는 정지 버튼 클릭을 기록 할 때 내가 클릭 기록 정지 버튼없이 기록을 계속하면, 그래서 stop() 방법

@Override 
public void stop() { 
    pairs.clear(); 
    pairs = null; 
    stopped = true; 
} 

을 실행합니다. 많은 메모리 누수가 발생합니다.

enter image description here

그래서, 나는 WeakReference이 옳다 사용 WeakReference 은 내가 메모리 릭을 방지하는 방법 생각이

//private ArrayList<OutputInputPair> pairs = new ArrayList<OutputInputPair(); 
    private ArrayList<WeakReference<OutputInputPair>> pairs = new ArrayList<WeakReference<OutputInputPair>>(); //global variable 

@Override 
public void add(OutputInputPair pair) { 
    //pairs.add(pair); 
    pairs.add(new WeakReference<OutputInputPair>(pair)); 
} 

@Override 
public void stop() { 
    pairs.clear(); 
    pairs = null; 
    stopped = true; 
} 

@Override 
public void process() { //record method 
    //for (OutputInputPair pair : pairs) { 
    for (WeakReference<OutputInputPair> pair = pairs) { 
     pair.output.fillCommandQueues(); //output is cannot resolve symbol message 
     pair.input.fillCommandQueues(); //input is cannot resolve symbol message 
    } 

    while (!stopped) { //when user click stop button, stopped = true. 
     //for (OutputInputPair pair : pairs) { 
     for (WeakReference<OutputInputPair> pair : pairs) { 
      recording(pair); //start recording 
     } 
    } 
    } 

public interface IOutputRaw { //IInputRaw class same code. 
    void fillCommandQueues(); 
} 

을 시도 사용하려면?

수정하는 방법 심볼 메시지의 weakreference 사용을 해결할 수 없습니까?

감사합니다.

public class OutputInputPair { 
    public IOutputRaw output; 
    public IInputRaw input; 

    public OutputInputPair(IOutputRaw output, IInputRaw input) { 
     this.output = output; 
     this.input = input; 
    } 
} 

답변

2

나는 WeakReference을 많이 모른다. 그러나 실제 참조를 얻으려면 get() 메서드를 사용해야합니다.

사용 :

if(pair == null) continue; 
OutputInputPair actualPair = pair.get(); 
if(actualPair == null) continue; 
actualPair.output.fillCommandQueues(); 
actualPair.input.fillCommandQueues(); 

대신 :

pair.output.fillCommandQueues(); 
pair.input.fillCommandQueues(); 
+0

'이 필요합니다 사용하기 전에 확인 null'. – Oleg

+0

@Oleg 감사합니다. –

+0

아니요, 쌍이 아니고 약하게 참조 된 객체가 지워진 경우'pair.get()'이 null을 반환 할 수 있습니다. – Oleg