Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28459 Discussions

Problem with association status of pointers

Paul_Felz
New Contributor I
231 Views

Please consider the following code-extracts

 

Module DS_Data
...
type :: HitCheck
    integer iLeftPix        ! linke Seite des Icons
    integer iTopPix         ! obere Kante des icon
    integer iRightPix       ! rechte Kante des Icons
    integer iBottomPix      ! untere Kante des Icons
    integer iArt            ! um welche Art Objekt handelt es sich
    integer iIndex          ! Index in der jeweiligen Objektliste
    type (HitCheck), pointer :: pNext
    type (HitCheck), pointer :: pPrior
end type

type (HitCheck), pointer :: ppHitHead, ppHitTail, ppHitActual

...

end module

...
...

case (WM_PAINT)

...
if (.not. associated (ppHitHead)) then
	allocate (ppHitHead)
      nullify (ppHitHead.pNext)
      nullify (ppHitHead.pPrior)
      ppHitTail => ppHitHead
      ppHitActual => ppHitHead
else
      allocate (ppHitActual.pNext)                                    ! Neue Werte werden einfach angehängt.
      ppHitActual.pNext.pPrior => ppHitActual
      ppHitActual => ppHitActual.pNext
      nullify (ppHitActual.pNext)
      ppHitTail => ppHitActual
endif
ppHitActual.iLeftPix = iXWin
ppHitActual.iTopPix = iYWin
ppHitActual.iRightPix = iXWin + iiIconViewSize
ppHitActual.iBottomPix = iYWin + iiIconViewSize
ppHitActual.iArt = EEG_ANLAGE
ppHitActual.iIndex = jKreis
...



case (WM_SIZE)

... 
lRslt = DeleteHitList()
...



logical function DeleteHitList()

use DS_Data
implicit none

DeleteHitList = .false.

if (associated (ppHitHead)) then                                         
    print *,'ppHitActual.iIndex', ppHitActual.iIndex
    do
        if (associated (ppHitActual.pNext)) then
            ppHitActual => ppHitActual.pNext
            print *,'im Do: ppHitActual.iIndex', ppHitActual.iIndex
            deallocate (ppHitActual.pPrior)
        else
            print *,'Done'
            exit
        endif
    enddo
!    deallocate (ppHitActual)                                           
!    if (associated (ppHitActual)) deallocate (ppHitActual) 
    nullify (ppHitActual)             
    llHitValid = .false.
    iiHitArt = 0
    DeleteHitList = .true.
endif

return
end

 

I coded the above to check if my mouse cursor hits objects in my window. This shows a map and the bitmaps indicate variuous items placed on it. The HitCheck objects should save the positions of these bitmaps and some further data.

Type declaration is in my module DS_Data, which is referenced in all my procedures. When a WM_PAINT occurs and the window gets drawn the rectangles are saved to a linked list of pointers to HitCheck-items. When the user resizes the window or takes other actions that change the positions of my items in the woindow, I want to delete the linked list to free memory and to build a new linked list with the new positions of my bitmaps.

Here is, when the error occurs:

My code creates an access violation with a second call to DeleteHitlist(). From my writeouts to a console I know this occurs on a second call to DeleteHitList() immediately after execution enters the loop. And this is what I do not understand. Either the pointer is associated, then it should not create an access violation error to disassociate it, or the pointer is not associated, then the function should not be executed.

Further: To my understanding after the loop is executed, there should be the actual pointer left. But when I tried to deallocate it - see the two lines commented out - apparently I hit some parts of memory vital for program execution and receive weird errors from other branches of my code.

I am under the impression I have som misunderstandings on how pointers work. Any hints?

Cheers

PF

0 Kudos
1 Reply
Steve_Lionel
Honored Contributor III
214 Views

ASSOCIATED returns valid results only if the pointer's association status is "defined". If the pointer's address is non-zero, ASSOCIATED will return .TRUE.. I don't see anything obvious in the excerpt here - my recommendation is to step through the code, watching the value of the pointer in the debug memory window and see if it changes only when it should. It could be that it is getting overwritten somewhere else in the program.

0 Kudos
Reply