Memory leaks 分配具有返回值的可分配多态变量时内存泄漏
我正在努力进行内存管理或正确使用面向对象的Fortran 2008。我的代码在Memory leaks 分配具有返回值的可分配多态变量时内存泄漏,memory-leaks,fortran,valgrind,Memory Leaks,Fortran,Valgrind,我正在努力进行内存管理或正确使用面向对象的Fortran 2008。我的代码在示例类模块中存储了具有相同父级的多个派生类型(此处为Example01和Example02)。我想根据某个变量在子例程workenv中加载一个示例(此处idx\u示例)。无论选择哪一个,都应该由基类中定义的过程访问,因此我可以执行调用active\u Example%Info()而不管active\u Example是什么 使用ifort(19.0.1.144)或gfortran(7.5.0)编译的以下代码按预期工作。
示例类
模块中存储了具有相同父级的多个派生类型(此处为Example01
和Example02
)。我想根据某个变量在子例程workenv
中加载一个示例(此处idx\u示例
)。无论选择哪一个,都应该由基类中定义的过程访问,因此我可以执行调用active\u Example%Info()
而不管active\u Example
是什么
使用ifort
(19.0.1.144)或gfortran
(7.5.0)编译的以下代码按预期工作。
但是valgrind
(3.13.0)警告内存泄漏(参见下面的代码)。如何防止内存泄漏并具有相同或类似的功能?(我希望在Example\u class
模块中拥有控件,并且在workenv
子例程中不使用多个if
-条件。)
使用ifort-free-stand f08示例。f-o示例时
valgrind ./Examples
==1689== Memcheck, a memory error detector
==1689== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1689== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==1689== Command: ./Examples
==1689==
==1689== Conditional jump or move depends on uninitialised value(s)
==1689== at 0x48CFC1: __intel_sse2_strcpy (in /mnt/Data/Examples)
==1689== by 0x45680E: for__add_to_lf_table (in /mnt/Data/Examples)
==1689== by 0x43C363: for__open_proc (in /mnt/Data/Examples)
==1689== by 0x42194D: for__open_default (in /mnt/Data/Examples)
==1689== by 0x42C092: for_write_seq_lis (in /mnt/Data/Examples)
==1689== by 0x40304D: example_baseclass_mp_info_ (in /mnt/Data/Examples)
==1689== by 0x402E17: MAIN__ (in /mnt/Data/Examples)
==1689== by 0x402A81: main (in /mnt/Data/Examples)
==1689==
Example Class 02
==1689==
==1689== HEAP SUMMARY:
==1689== in use at exit: 32 bytes in 1 blocks
==1689== total heap usage: 11 allocs, 10 frees, 12,775 bytes allocated
==1689==
==1689== LEAK SUMMARY:
==1689== definitely lost: 0 bytes in 0 blocks
==1689== indirectly lost: 0 bytes in 0 blocks
==1689== possibly lost: 0 bytes in 0 blocks
==1689== still reachable: 32 bytes in 1 blocks
==1689== suppressed: 0 bytes in 0 blocks
==1689== Rerun with --leak-check=full to see details of leaked memory
==1689==
==1689== For counts of detected and suppressed errors, rerun with: -v
==1689== Use --track-origins=yes to see where uninitialised values come from
==1689== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
使用gfortran-ffree form-std=f2008示例。f-o示例
:
valgrind ./Examples
==1729== Memcheck, a memory error detector
==1729== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1729== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==1729== Command: ./Examples
==1729==
Example Class 02
==1729==
==1729== HEAP SUMMARY:
==1729== in use at exit: 20 bytes in 1 blocks
==1729== total heap usage: 23 allocs, 22 frees, 13,536 bytes allocated
==1729==
==1729== LEAK SUMMARY:
==1729== definitely lost: 20 bytes in 1 blocks
==1729== indirectly lost: 0 bytes in 0 blocks
==1729== possibly lost: 0 bytes in 0 blocks
==1729== still reachable: 0 bytes in 0 blocks
==1729== suppressed: 0 bytes in 0 blocks
==1729== Rerun with --leak-check=full to see details of leaked memory
==1729==
==1729== For counts of detected and suppressed errors, rerun with: -v
==1729== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
但是valgrind(3.13.0)警告内存泄漏我认为这可能是valgrind的问题,而不是代码问题,valgrind没有很好地利用Fortran的未来特性。我肯定我在这里看到过Qs和这种效果,但是我能在需要的时候找到它们吗?真见鬼!谢谢你的调查。这绝对是一种可能性,因为乍一看一切似乎都正常。我只是不希望在所有东西都在扩展时出现大量内存问题。同样的20字节丢失也发生在我的机器上(MacOS10.11上的gfortran-9.2)。如果我将函数“Select_Example()”更改为带有伪类变量“myExample”的子例程,那么泄漏就会消失……我可以用gfortran确认此行为(使用子例程时没有泄漏报告)。有趣的是,当使用ifort编译时,valgrind仍然报告相同的内存泄漏(这次是9个alloc和8个frees)。但是valgrind(3.13.0)警告内存泄漏。我认为这可能是valgrind的问题,而不是代码问题,valgrind不能很好地使用Fortran的未来特性。我肯定我在这里看到过Qs和这种效果,但是我能在需要的时候找到它们吗?真见鬼!谢谢你的调查。这绝对是一种可能性,因为乍一看一切似乎都正常。我只是不希望在所有东西都在扩展时出现大量内存问题。同样的20字节丢失也发生在我的机器上(MacOS10.11上的gfortran-9.2)。如果我将函数“Select_Example()”更改为带有伪类变量“myExample”的子例程,那么泄漏就会消失……我可以用gfortran确认此行为(使用子例程时没有泄漏报告)。有趣的是,当使用ifort编译时,valgrind仍然报告相同的内存泄漏(这次有9个alloc和8个free)。
valgrind ./Examples
==1729== Memcheck, a memory error detector
==1729== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1729== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==1729== Command: ./Examples
==1729==
Example Class 02
==1729==
==1729== HEAP SUMMARY:
==1729== in use at exit: 20 bytes in 1 blocks
==1729== total heap usage: 23 allocs, 22 frees, 13,536 bytes allocated
==1729==
==1729== LEAK SUMMARY:
==1729== definitely lost: 20 bytes in 1 blocks
==1729== indirectly lost: 0 bytes in 0 blocks
==1729== possibly lost: 0 bytes in 0 blocks
==1729== still reachable: 0 bytes in 0 blocks
==1729== suppressed: 0 bytes in 0 blocks
==1729== Rerun with --leak-check=full to see details of leaked memory
==1729==
==1729== For counts of detected and suppressed errors, rerun with: -v
==1729== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)