Cap.release() cv2.destroyAllWindows() crash on Mac

Hi! everytime i press the “q” on the keyboard the camera freezes and crashes. Any advice on why this happens?

cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()
        
    cv2.imshow("Image Collection", frame)
    
    if cv2.waitKey(1) & 0XFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Thank you very much!!

Python: 3.9