UDDTIO读取​​:未释放指针被释放

时间:2016-08-06 17:02:06

标签: fortran derived-types

我正在尝试将使用ifort编译器版本15在Fortran中执行用户定义的派生类型输入输出的最小示例放在一起。下面发布的代码能够相应地读取和写入,但是在执行“read”之后当控件返回主程序时,会发生以下错误:

(61586,0x7fff7e4dd300) malloc: *** error for object 0x10fa7bac4: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

UDDTIO例程在程序中被调用为:

module mod_test

    implicit none

    type, public :: class_test
        integer :: foo
    contains
        procedure, private :: write_test
        procedure, private :: read_test
        generic :: write(formatted) => write_test
        generic :: read(formatted)  => read_test
    end type class_test

contains

    subroutine     write_test(dtv, unit, iotype, v_list, iostat, iomsg)
        !
        implicit none
        !
        class(class_test), intent(in) :: dtv
        integer     , intent(in)    :: unit
        character(*), intent(in)    :: iotype
        integer     , intent(in)    :: v_list(:)
        integer     , intent(out)   :: iostat
        character(*), intent(inout) :: iomsg
        !
        iostat = 0
        !
        write(unit,'(a,/)') '<foo>'
        write(unit,*) dtv%foo
        write(unit,'(/)')
        write(unit,'(a,/)') '</foo>'
        !
    end subroutine write_test

    subroutine     read_test(dtv, unit, iotype, v_list, iostat, iomsg)
        !
        implicit none
        !
        class(class_test), intent(inout) :: dtv
        integer     , intent(in)    :: unit
        character(*), intent(in)    :: iotype
        integer     , intent(in)    :: v_list(:)
        integer     , intent(out)   :: iostat
        character(*), intent(inout) :: iomsg
        !
        read(unit,'(/)')
        read(unit,*) dtv%foo
        read(unit,'(/)')
        read(unit,'(/)')
        write(*,*) 'z'
    end subroutine read_test

end module mod_test

program main

    use mod_test

    implicit none

    type(class_test) :: test

    test%foo = 5

    write(*,*) 'writing'
    open(unit=1, file='write.out', status='replace', action='write')
        write(unit=1,fmt=*) test
    close(unit=1)

    write(*,*) 'reading'
    open(unit=1, file='write.out', status='old', action='read')
        read(unit=1,fmt=*) test
    close(unit=1)

    write(*,*) 'end'

end program main

问题似乎在于换行符'(/)'。 将iostat = -1添加到读取例程似乎“解决”了问题。但为什么呢?

1 个答案:

答案 0 :(得分:2)

需要UDDTIO过程来定义q = 'Trump' twitter_stream = twitter.TwitterStream(auth=twitter_api.auth) stream = twitter_stream.statuses.filter(track=q) dates = [tweet['created_at'] for tweet in stream] with open('dates.json', 'a') as outfile: json.dump(dates, outfile, indent=4) 伪参数,以便编译器的运行时可以测试错误并采取适当的操作。

请注意,UDDTIO是该编译器最近实现的功能。您需要使用最新的编译器版本。

如果我在读取和写入UDDTIO过程中正确定义iostat伪参数,那么您的代码将以当前的17.0 beta运行。

进一步的UDDTIO问题应该在17.0的初始版本中修复,这不能太远。