Image Layout Transition Problem

I encountered an image layout transition problem. My whole commands are basically:

  1. Create VkImage things with initial layout Undefined
  2. CommandBuffer0: image layout transition undefined → shader_read_only_optimal
  3. CommandBuffer1:
    3.1 image layout transition shader_read_only_optimal->general
    3.2 access by a Compute Shader as a storage image
    3.3 image layout transition general->shader_read_only_optimal
    3.4 access by another Compute Shader as a sampled image
  4. CommandBuffer2, do exactly what commandbuffer1 did
  5. CommandBuffer1, …
  6. CommandBuffer2, …

Whenever I handle a window resize, I will repeat the whole process, and it seems that it works fine for most time, but sometimes the validation layer will report error as follows:

Command buffer expects VkImage (subresource: aspectMask 0x1 array layer 0, mip level 0) to be in layout VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL–instead, current layout is VK_IMAGE_LAYOUT_GENERAL.

In visual studio debug mode, the error will not happen until the first submit of CommandBuffer2, so I guess the image is successfully changed from undefined to shader_read_only_optimal and then to general, but never be able to change back to shader_read_only_optimal?

However, in Nsight Graphics, the error will also exist when I capture the frame and find the barrier is submitted correctly.

I have no idea why, if the general->shader_read_only_optimal transition is not successful, why there is no error for 3.4 where it is actually accessed as a sampled image? Please help me, thanks~

By the way, though may not be related to the error…
I submit the commandBuffer 0 and then immediately do logical device waitIdle.
Will the image layout transition be done?

I mean, as the dstPipeline Stage is set to ComputeShader for example, but I only submit a barrier with image memory barrier, without any following compute shader access the image, will the image transition finish before waitIdle() returns or … waiting for some point right before a compute shader is going to be dispatched?