0

I have a fairly standard loop for reading in from a file in binary:

char* buffer;    
if(imageData.is_open())
{
    imageData.seekg(0, std::ios::end);
    int filelen = imageData.tellg();
    entrysize = filelen/(hdrDimensions[0] * hdrDimensions[1] * hdrDimensions[2] * hdrDimensions[3]);
    buffer = new char[sizeof(short int)];
    if(entrysize == 2)
    {
        imgDataShort = new std::vector<short int>((hdrDimensions[0] * hdrDimensions[1] * hdrDimensions[2] * hdrDimensions[3]), 0);
        imageData.seekg(0, std::ios::beg);
        int j(0);
        for(int i=0; i < (hdrDimensions[0] * hdrDimensions[1] * hdrDimensions[2] * hdrDimensions[3]); i++)
        {
            imageData.read(reinterpret_cast<char*>(buffer), sizeof(short int));
            memcpy(&(imgDataShort->at(i)), buffer, sizeof(short int));
            j += sizeof(short int);
        }
    }
    delete [] buffer;
}

in this case, reading a CT image. I have come across a strange problem whereby this loop hangs about halfway through the file (the file is 30MB), going into disk sleep. I have absolutely no idea why this is happening.

Details:

Just in case it is important, I'm running this on an Intel XeonPhi card which is running busybox, and the file is being read from an nfs mount. The exact same code runs fine on a 'normal' machine. I have no control over the nfs configuration, but if something is broken, obviously I can ask for it to be fixed. The mount config line looks like (taken from df -h):

rw,relatime,vers=3,rsize=32768,wsize=32768,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=XXX.XXX.XXX.XXX,mountvers=3,mountport=300,mountproto=udp,local_lock=none,addr=XXX.XXX.XXX.XXX
Theolodus
  • 2,004
  • 1
  • 23
  • 30
  • 1
    That loop has so much dynamic memory management. You need to post a real function, and a real call to it to see if you're mismanaging memory. But seriously, are all of those calls to `new[]` really necessary? What purpose does `imageDataShort` have outside of this code? And why isn't buffer a `std::vector` or some other RAII type? If for some reason, that `delete [] buffer` is never reached, you have a memory leak. To whit: `imgDataShort->at(i)), ` This can throw an exception, completely missing your `delete [] buffer` line. – PaulMcKenzie Apr 30 '14 at 15:52
  • You need to add error testing and reporting to your code. For example, did you forget to open the file as `ios::binary` and encounter a value of CTRL-Z (an EOF character in Windows text files). – Thomas Matthews Apr 30 '14 at 16:09

0 Answers0