I'm working on porting some legacy code from CVF to IVF. I don't have an extensive computer science background, so I was wondering if someone could explain what's happening in the following situation. Hopefully psuedocode will suffice.
Type( Results_Record ), Pointer:: Results_rec(:)
Type( Results_Record ), Pointer :: Filtered_results1(:)
Type( Results_Record ), Pointer :: Filtered_results2(:)
DO I = 1, N
In a subroutine:
If necessary, deallocate Results_rec
Allocate results_rec and initialize it
Fill Results_rec
Return Results_rec
End Subroutine
If filter 1 is applied to the results
In another subroutine:
If necessary deallocate Filtered_results1
Allocate and initialize Filtered_results1
Fill Filtered_results1 from Results_rec
Deallocate, allocate and initialize Results_rec
Results_rec => Filtered_results1
return Results_rec
End subroutine
End IF
If filter 2 is applied to Results_rec
In a subroutine:
If necessary deallocate Filtered_results2
Allocate and initialize Filtered_results2
Fill Filtered_results2 from Results_rec
Deallocate, allocate and initialize Results_rec
Results_rec => Filtered_results2
Return Results_rec
End subroutine
End IF
Output Results_rec
End do
If filter 1 or filter 2 is applied the code works fine. If both filter 1 and filter 2 are applied to the results the code will throw a fatal error the second time through. VS2010 gives a run-time error that says "Damage before (a memory address) which was allocated by aligned routine", then "debug assertion failed with an invalid signal error". If I watch the code in the debugger the second time through the loop allocating space for Filtered_results1 destroys the results in Results_rec. I don't think the deallocate statement is executing since the memory would've been deallocated and the pointer's association would've been broken in the second filter the first time through the loop. This implies that, even though results_rec has been deallocated, allocated and initialized twice before returning to the first filter it's somehow using the same memory location as Filtered_result1. I should note that this works in Compaq Visual Fortran. Can someone explain what's happening and give any suggestions for how to fix the code?



