BinaryWriters automatically dispose the MemoryStream it is using when the BinaryWriter is disposed of; the fix is to Flush instead of Dispose when finished (and let the GC handle disposing when it is appropriate to do so).
pk3DS: A package of Pokémon X/Y/OR/AS ROM Editing Tools by various contributors.