If I had to guess, I'd say you are not cleaning up your shared memory. Is this a unix system? Use the ipcs command to show all the shared memory. If you set up the shm once, and then don't clean it up, the second time it might fail, depending on how you try to access it.
To find out what the error message means, first, determine which system call returned the error. Then, read the man page under ERRORS to see what conditions cause that error to be set.