problem attaching to drives at boot time

I’m writing my first filter driver using filespy as base. in my filter
driver i’m attaching my driver with the logical drives present in the
system and then filter the delete, read and write requests for certain
files.
the start type of the driver is Demand Start.
it works fine when i install it for first time. but the problem comes when
i reboot the system, it gives the Access violation - code c0000005 error
and the system hangs.
i traced it and found that the API IoAttachDeviceToDeviceStack fails. i
have checked everything but found no reason for it. the same code works
prefectly when starting for the first time. i’m using the code given in
filespy example to attach to drives.
is it because of the order in which the drivers load or can there be any
race condition among the drivers attaching to logical drives at boot time.
any help will be greatly appreciated.

regards
Balvinder