fix(memory): утечки ObjectURL в 4 компонентах (22.09.2026)
This commit is contained in:
@@ -209,16 +209,21 @@ const FeedPostComposer: React.FC<FeedPostComposerProps> = ({
|
||||
setSuccess(false);
|
||||
}, [isOpen, editingPost, language]);
|
||||
|
||||
// 🔧 ФИКС: revoke ObjectURL при размонтировании
|
||||
// 🔧 ФИКС (22.09.2026): утечка ObjectURL при размонтировании.
|
||||
// Было: setSlots в cleanup — setState в unmounted компоненте НЕ выполняется,
|
||||
// поэтому URL.revokeObjectURL никогда не вызывался.
|
||||
// Стало: ref с актуальными слотами + чтение из ref в cleanup.
|
||||
const slotsRef = useRef<(ImageSlot | null)[]>([null, null, null]);
|
||||
useEffect(() => {
|
||||
slotsRef.current = slots;
|
||||
}, [slots]);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
setSlots(prev => {
|
||||
prev.forEach(slot => {
|
||||
if (slot?.previewUrl) {
|
||||
try { URL.revokeObjectURL(slot.previewUrl); } catch { /* ignore */ }
|
||||
}
|
||||
});
|
||||
return prev;
|
||||
slotsRef.current.forEach(slot => {
|
||||
if (slot?.previewUrl) {
|
||||
try { URL.revokeObjectURL(slot.previewUrl); } catch { /* ignore */ }
|
||||
}
|
||||
});
|
||||
};
|
||||
}, []);
|
||||
|
||||
@@ -45,11 +45,20 @@ const PronunciationModal: React.FC<PronunciationModalProps> = ({ isOpen, onClose
|
||||
...extra,
|
||||
});
|
||||
|
||||
// 🔧 ФИКС (22.09.2026): утечка ObjectURL. Каждый playWord создавал blob-URL
|
||||
// без revoke. Добавили audioUrlRef — хранит текущий URL для очистки.
|
||||
const audioUrlRef = useRef<string | null>(null);
|
||||
|
||||
const stopAudio = () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current = null;
|
||||
}
|
||||
// 🔧 Освобождаем предыдущий blob-URL
|
||||
if (audioUrlRef.current) {
|
||||
try { URL.revokeObjectURL(audioUrlRef.current); } catch { /* ignore */ }
|
||||
audioUrlRef.current = null;
|
||||
}
|
||||
if ('speechSynthesis' in window) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
@@ -61,6 +70,7 @@ const PronunciationModal: React.FC<PronunciationModalProps> = ({ isOpen, onClose
|
||||
try {
|
||||
const audioBlob = await getTTS(word, 'male');
|
||||
const audioUrl = URL.createObjectURL(audioBlob);
|
||||
audioUrlRef.current = audioUrl;
|
||||
const audio = new Audio(audioUrl);
|
||||
audioRef.current = audio;
|
||||
audio.onended = () => setIsPlaying(false);
|
||||
|
||||
@@ -64,6 +64,25 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
|
||||
const [isPhraseLoading, setIsPhraseLoading] = useState(false);
|
||||
const [isPlayingPhrase, setIsPlayingPhrase] = useState(false);
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||
|
||||
// 🔧 ФИКС (22.09.2026): утечка ObjectURL в handlePlayPhrase, handlePlayTranslate, handlePlayLetter.
|
||||
// Каждый play создавал blob-URL без revoke. Добавили audioUrlRef + stopAudioHelper.
|
||||
const audioUrlRef = useRef<string | null>(null);
|
||||
|
||||
const stopAudioHelper = () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current = null;
|
||||
}
|
||||
if (audioUrlRef.current) {
|
||||
try { URL.revokeObjectURL(audioUrlRef.current); } catch { /* ignore */ }
|
||||
audioUrlRef.current = null;
|
||||
}
|
||||
if ('speechSynthesis' in window) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
};
|
||||
|
||||
const scrollContainerRef = useRef<HTMLDivElement>(null);
|
||||
const menuTrackedRef = useRef(false);
|
||||
|
||||
@@ -132,6 +151,13 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
|
||||
onClose();
|
||||
};
|
||||
|
||||
// 🔧 ФИКС (22.09.2026): очистка audio при размонтировании
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
stopAudioHelper();
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleOpenLetter = (index: number) => {
|
||||
setSelectedLetter(index);
|
||||
scrollToTop();
|
||||
@@ -250,20 +276,13 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
|
||||
const handlePlayPhrase = async () => {
|
||||
if (!phraseOfDay || isPlayingPhrase) return;
|
||||
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current = null;
|
||||
}
|
||||
|
||||
if ('speechSynthesis' in window) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
|
||||
stopAudioHelper();
|
||||
setIsPlayingPhrase(true);
|
||||
|
||||
try {
|
||||
const audioBlob = await getTTS(phraseOfDay.phrase_pl, 'male');
|
||||
const audioUrl = URL.createObjectURL(audioBlob);
|
||||
audioUrlRef.current = audioUrl;
|
||||
const audio = new Audio(audioUrl);
|
||||
audioRef.current = audio;
|
||||
audio.onended = () => setIsPlayingPhrase(false);
|
||||
@@ -296,20 +315,13 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
|
||||
const handlePlayTranslate = async () => {
|
||||
if (!translateResult || isPlaying) return;
|
||||
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current = null;
|
||||
}
|
||||
|
||||
if ('speechSynthesis' in window) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
|
||||
stopAudioHelper();
|
||||
setIsPlaying(true);
|
||||
|
||||
try {
|
||||
const audioBlob = await getTTS(translateResult, 'male');
|
||||
const audioUrl = URL.createObjectURL(audioBlob);
|
||||
audioUrlRef.current = audioUrl;
|
||||
const audio = new Audio(audioUrl);
|
||||
audioRef.current = audio;
|
||||
audio.onended = () => setIsPlaying(false);
|
||||
@@ -321,17 +333,12 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
|
||||
};
|
||||
|
||||
const handlePlayLetter = async (word: string) => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current = null;
|
||||
}
|
||||
if ('speechSynthesis' in window) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
stopAudioHelper();
|
||||
setIsPlaying(true);
|
||||
try {
|
||||
const audioBlob = await getTTS(word, 'male');
|
||||
const audioUrl = URL.createObjectURL(audioBlob);
|
||||
audioUrlRef.current = audioUrl;
|
||||
const audio = new Audio(audioUrl);
|
||||
audioRef.current = audio;
|
||||
audio.onended = () => setIsPlaying(false);
|
||||
|
||||
@@ -43,11 +43,18 @@ const TypingModal: React.FC<TypingModalProps> = ({ isOpen, onClose }) => {
|
||||
...extra,
|
||||
});
|
||||
|
||||
// 🔧 ФИКС (22.09.2026): утечка ObjectURL в playAnswer.
|
||||
const audioUrlRef = useRef<string | null>(null);
|
||||
|
||||
const stopAudio = () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current = null;
|
||||
}
|
||||
if (audioUrlRef.current) {
|
||||
try { URL.revokeObjectURL(audioUrlRef.current); } catch { /* ignore */ }
|
||||
audioUrlRef.current = null;
|
||||
}
|
||||
if ('speechSynthesis' in window) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
@@ -146,13 +153,14 @@ const TypingModal: React.FC<TypingModalProps> = ({ isOpen, onClose }) => {
|
||||
setShowAnswer(true);
|
||||
};
|
||||
|
||||
const playAnswer = async () => {
|
||||
const playAnswer = async () => {
|
||||
if (!correctAnswer) return;
|
||||
stopAudio();
|
||||
setIsPlaying(true);
|
||||
try {
|
||||
const audioBlob = await getTTS(correctAnswer, 'male');
|
||||
const audioUrl = URL.createObjectURL(audioBlob);
|
||||
audioUrlRef.current = audioUrl;
|
||||
const audio = new Audio(audioUrl);
|
||||
audioRef.current = audio;
|
||||
audio.onended = () => setIsPlaying(false);
|
||||
|
||||
Reference in New Issue
Block a user