Fatal Error on Memory Allocation

Fatal Error on Memory Allocation

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?


publicaciones de 4 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.

So I think the problem is I deallocate the memory allocated to Filtered_results1 through the pointer Results_rec when I go through the second filter. Then, when I enter the first filter on the second pass the pointer Filtered_results1 has maintained its association. Deallocating it attempts to deallocate memory that's already been deallocated, even though the debugger shows random values in the record instead of null pointer. If I only deallocate memory through the pointer Results_rec, will this cause a memory leak? I'm worried that Filtered_results1 will still have memory associated with it, even though the values shown in the debugger are garbage.

>>...If I only deallocate memory through the pointer Results_rec, will this cause a memory leak?..

In practice when it is applied properly it will do the opposite, that is, it will deallocate the memory and it won't create the memory leaks.

In pseudo-codes:
If( PointerToSomeMemory is NULL ) Then
PointerToSomeMemory = DoAllocateMemory(...)
Some Processing
If( PointerToSomeMemory is NOT NULL ) Then
DoDeallocateMemory( PointerToSomeMemory )
PointerToSomeMemory = NULL

And don't forget

OtherPointer => SomePointer
SomePointer = NULL ! when pointer no longer valid (same rule as in C/C++)

Else Sergey's suggestion might lead to unintended deletion of an in-use object

Jim Dempsey


Inicie sesión para dejar un comentario.