Flash in the micro is arranged in blocks.. You need to write / erase complete blocks at a time..
If you grab a datasheet for one of their chips, there is a software sequence that needs to be undertaken for a successful flash write or erase.
First all bytes are written to the sram ( a complete block , 64 bytes on a pic18).
Then the EECON registers are setup.
Then you disable all interrupts.
Then you write 0x55.
Then you write 0xAA.
Then set the write flag.
(When writing or reading flash... NOTHING else can be done until it's finished).
If you use flash for in application use, be prepared for lockups..