Multithreading 覆盖缓冲区仍然会导致内存崩溃

Multithreading 覆盖缓冲区仍然会导致内存崩溃,multithreading,python-2.7,opencv,Multithreading,Python 2.7,Opencv,我正在尝试将AVI视频转换为一系列图像,只将最近8秒的视频保存到“缓冲区”中。我只保存了两位信息:从AVI中提取图像的时间戳,以及图像本身 我的问题是,即使程序是为缓冲区编写的,以覆盖其自身,它仍然会发生内存崩溃-对于8s缓冲区,大约2分钟。(错误:(-4)未能在函数cv::OutofMemoryError中分配########字节)此线程的代码位于上下文的旁边 (上下文:我正在尝试为帧抓取器创建一个模拟器。它不断/无休止地转换保存的AVI,并在视频结束时重新启动。缓冲区应该类似于帧抓取器实际保

我正在尝试将AVI视频转换为一系列图像,只将最近8秒的视频保存到“缓冲区”中。我只保存了两位信息:从AVI中提取图像的时间戳,以及图像本身

我的问题是,即使程序是为缓冲区编写的,以覆盖其自身,它仍然会发生内存崩溃-对于8s缓冲区,大约2分钟。(错误:(-4)未能在函数cv::OutofMemoryError中分配########字节)此线程的代码位于上下文的旁边

(上下文:我正在尝试为帧抓取器创建一个模拟器。它不断/无休止地转换保存的AVI,并在视频结束时重新启动。缓冲区应该类似于帧抓取器实际保存到的任何硬件-HDD或SSD或任何硬件。该模拟器作为自己的thr运行ead,与另一个线程并行,该线程根据请求处理和发送图像。崩溃发生时不调用第二个线程。)

我猜这里可能有一个非常简单的逻辑问题,防止缓冲区实际上覆盖自身,或者其他变量“挂起”,连续运行累积并导致程序崩溃。否则,有没有更好的方法来处理这个问题,从而避免这个错误


谢谢!

您是否递归调用
VidEmulator()
?!顺便说一句,我看不到任何
t2
,但是
t1.join()
看起来像是一个直接的死锁。是的,当达到最大缓冲区大小(fps*8)时,它会将计数设置为1并再次调用VidEmulator()。我忽略了t2,因为它在崩溃发生之前没有被调用。(为了测试,我启动了程序,让它继续运行。t1连续运行,t2根据请求运行,所以这就是我在这里的实际情况。)我将在编辑中删除t2…然后,因为
VidEmulator()
基本上不会返回,所以将有一个无休止的递归运行,直到(堆栈)内存耗尽。同样,通过这种方式,您可以一遍又一遍地执行
video=cv2.VideoCapture(vidfile)
。不确定这是否是您想要的。然后,在
framearray=[0]*numbertosave
帧收集完成后,您还可以完全丢弃缓冲区中的内容。-简言之:删除递归。
import base64, os, cv2, re, time, math
from PIL import Image
from threading import Thread

lobal numbertosave, framearray, sessionlist
framearray = []
vidfile = os.path.normpath("videolocation")
savepath = os.path.normpath("whereIsaverequestedimages"

class FrameInfo:
    def __init__(self, frame, time):
        self.f = frame
        self.t = time

def VidEmulator():
    global numbertosave, framearray, hit, fps
    video = cv2.VideoCapture(vidfile)
    fps = video.get(5)
    ##### Change the integer in the expression below to the number of seconds you want ot save
    numbertosave = int(fps*5)
    ######## numbertosave = framerate * (# of seconds to store in buffer)
    totalframes = video.get(7)
    atframe = 1
    count = 1
    framearray = [0] * numbertosave
    deltat = []
    converting = True
    while converting == True:
        try:
            ret, frame = video.read()
            timestamp = time.time()
            x = FrameInfo(frame, timestamp)
            framearray[count] = ((x.f, x.t))
            atframe = atframe + 1
            count = count + 1
            if count == numbertosave:
                count = 1
                VidEmulator()
        except KeyboardInterrupt:
            #### edited to remove mention of other thread, which isn't
            #### invoked when testing this one (it is on request; see
            #### comments) I honestly haven't tested this block since I
            #### usually ctrl+printbreak
            t1.join()
            print "threads killed"
            exit()
            print "hopefully you don't see this"

if __name__ == '__main__':
    t1 = Thread(target = VidEmulator)
    t1.start()