I realised the problem was I think because the pointer to imageBuffer was somehow deallocated by the time it tried to execute the model inference. I was passing a pointer to imageBuffer from swift to C++. Somehow I am not sure why imageBuffer seems to be deallocated when I called the inference code from swift. I fixed it by defining a @State property for the imageBuffer variable in swift