异常代码 C++ 中的泄漏

我一直在处理一个学校项目,其中一项任务是确保它完全不泄漏.所以,我通过 valgrind 运行了我的程序,因为我没有使用任何动态内存分配,所以我认为我找不到任何东西.

糟糕,我做到了.Valgrind 给了我这个:

==22107== 1 个块中的 16 个字节在丢失记录 1 of 4 中肯定丢失==22107== 在 0x100038915:malloc (vg_replace_malloc.c:236)==22107== 由 0x1000950CF:__cxa_get_globals(在/usr/lib/libstdc++.6.0.9.dylib 中)==22107== 由 0x100094DCD:__cxa_allocate_exception(在/usr/lib/libstdc++.6.0.9.dylib 中)==22107== 由 0x100051D42: std::__throw_out_of_range(char const*) (在/usr/lib/libstdc++.6.0.9.dylib 中)==22107== by 0x100005463: std::vector>, std::allocator>>>::_M_range_check(unsigned long) const(在 ./connect3 中)==22107== by 0x100005482: std::vector>, std::allocator>>>::at(unsigned long)(在 ./connect3 中)==22107== 由 0x1000016E3:connect3::checkIfPositionIsBaseCase(Position) const(在 ./connect3 中)==22107== by 0x100007BD8:Game::evaluate(Position)(在./connect3中)==22107== by 0x100007D72:Game::evaluate(Position)(在./connect3中)==22107== by 0x1000043B4: main (in ./connect3)==22107====22107== 泄漏摘要:==22107== 肯定丢失了:1 个块中的 16 个字节==22107== 间接丢失:0 个块中的 0 个字节==22107== 可能丢失:0 个块中的 0 个字节==22107== 仍然可以访问:3 个块中的 8,280 字节==22107== 被抑制:0 个块中的 0 个字节==22107== 可到达的块(找到指针的块)未显示.==22107== 要查看它们,请重新运行:--leak-check=full --show-reachable=yes

好吧,我看了一下它来自我的函数checkIfPositionIsBaseCase(Position)".看着这个方法(我的伙伴写的),我实际上很惊讶地看到了可能导致泄漏的东西.

例外.这是该函数的代码.(自始至终几乎是一样的,阅读第一个 try catch 并且您已经阅读了所有内容).

///////checkIfPositionIsBaseCase///bool connect3::checkIfPositionIsBaseCase(Position aPosition) const {向量<向量<国际 >>thisP = aPosition.getBoard();for( int w = 0; w < thisP.size(); w++ ) {for( int h = 0; h < thisP.at(w).size(); h++ ){int thisS = thisP.at( w ).at( h );如果(这S!= 0){尝试{if( thisP.at( w - 1 ).at( h - 1 ) == thisS ){if( thisP.at( w - 2 ).at( h - 2 ) == thisS ){返回真;}}}catch(out_of_range&){}尝试{if( thisP.at( w ).at( h - 1 ) == thisS ){if( thisP.at( w ).at( h - 2 ) == thisS ){返回真;}}}catch(out_of_range&){}尝试{if( thisP.at( w + 1 ).at( h - 1 ) == thisS ){if( thisP.at( w + 2 ).at( h - 2 ) == thisS ){返回真;}}}catch(out_of_range&){}尝试{if( thisP.at( w - 1 ).at( h ) == thisS ){if( thisP.at( w - 2 ).at( h ) == thisS ){返回真;}}}catch(out_of_range&){}尝试{if( thisP.at( w + 1 ).at( h ) == thisS ){if( thisP.at( w + 2 ).at( h ) == thisS ){返回真;}}}catch(out_of_range&){}尝试{if( thisP.at( w - 1 ).at( h + 1 ) == thisS ){if( thisP.at( w - 2 ).at( h + 2 ) == thisS ){返回真;}}}catch(out_of_range&){}尝试{if( thisP.at( w ).at( h + 1 ) == thisS ){if( thisP.at( w ).at( h + 2 ) == thisS ){返回真;}}}catch(out_of_range&){}尝试{if( thisP.at( w + 1 ).at( h + 1 ) == thisS ){if( thisP.at( w + 2 ).at( h + 2 ) == thisS ){返回真;}}}catch(out_of_range&){}}}}//////一种可能///for (int i = 0; i < thisP.size(); i++) {for (int j = 0; j 

我读了一点,看起来我捕获异常的事实意味着我正在泄漏内存,但我不知道如何解决这个问题.如何重构代码,以免内存泄漏?

解决方案

了解反复泄漏内存(可能导致耗尽)与拥有一些底层支持代码或库之间的区别很重要-关闭初始化步骤,获取一些将在程序运行时使用的堆内存(在这种情况下,在程序终止时释放/删除内存并不是真正有用或必要的,尝试安排它可能会很麻烦).

这里,__cxa_get_globals 似乎在做一次性 malloc.

小故事:当重复调用这些异常时,请确保您不会获得多个未发布的块(或更大的块)....

I've been working with a school project, and one of the tasks is to make sure it doesn't leak at all. So, I ran my program through valgrind, and because I'm not using any dynamic memory allocation, I didn't think I would find anything.

Oops, I did. Valgrind gave me this:

==22107== 16 bytes in 1 blocks are definitely lost in loss record 1 of 4
==22107==    at 0x100038915: malloc (vg_replace_malloc.c:236)
==22107==    by 0x1000950CF: __cxa_get_globals (in /usr/lib/libstdc++.6.0.9.dylib)
==22107==    by 0x100094DCD: __cxa_allocate_exception (in /usr/lib/libstdc++.6.0.9.dylib)
==22107==    by 0x100051D42: std::__throw_out_of_range(char const*) (in /usr/lib/libstdc++.6.0.9.dylib)
==22107==    by 0x100005463: std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_range_check(unsigned long) const (in ./connect3)
==22107==    by 0x100005482: std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::at(unsigned long) (in ./connect3)
==22107==    by 0x1000016E3: connect3::checkIfPositionIsBaseCase(Position) const (in ./connect3)
==22107==    by 0x100007BD8: Game::evaluate(Position) (in ./connect3)
==22107==    by 0x100007D72: Game::evaluate(Position) (in ./connect3)
==22107==    by 0x1000043B4: main (in ./connect3)
==22107== 
==22107== LEAK SUMMARY:
==22107==    definitely lost: 16 bytes in 1 blocks
==22107==    indirectly lost: 0 bytes in 0 blocks
==22107==      possibly lost: 0 bytes in 0 blocks
==22107==    still reachable: 8,280 bytes in 3 blocks
==22107==         suppressed: 0 bytes in 0 blocks
==22107== Reachable blocks (those to which a pointer was found) are not shown.
==22107== To see them, rerun with: --leak-check=full --show-reachable=yes

Well, I took a look at it is coming from my function "checkIfPositionIsBaseCase(Position)". Looking at this method (which my partner wrote), I was actually surprised to see something which may have caused the leak.

Exceptions. Here is the code for that function. (It's pretty much the same thing through out, read the first try catch and you've read them all).

///
/// checkIfPositionIsBaseCase
///
bool connect3::checkIfPositionIsBaseCase(Position aPosition) const {

    vector< vector< int > > thisP = aPosition.getBoard();

    for( int w = 0; w < thisP.size(); w++ ) {
        for( int h = 0; h < thisP.at(w).size(); h++ ){
            int thisS = thisP.at( w ).at( h );
            if( thisS != 0 ){
                try{
                    if( thisP.at( w - 1 ).at( h - 1 ) == thisS ){
                        if( thisP.at( w - 2 ).at( h - 2 ) == thisS ){
                            return true;
                        }
                    }
                }catch( out_of_range& ){}

                try{
                    if( thisP.at( w ).at( h - 1 ) == thisS ){
                        if( thisP.at( w ).at( h - 2 ) == thisS ){
                            return true;
                        }
                    }
                }catch( out_of_range& ){}

                try{
                    if( thisP.at( w + 1 ).at( h - 1 ) == thisS ){
                        if( thisP.at( w + 2 ).at( h - 2 ) == thisS ){
                            return true;
                        }
                    }
                }catch( out_of_range& ){}

                try{
                    if( thisP.at( w - 1 ).at( h ) == thisS ){
                        if( thisP.at( w - 2 ).at( h ) == thisS ){
                            return true;
                        }
                    }
                }catch( out_of_range& ){}

                try{
                    if( thisP.at( w + 1 ).at( h ) == thisS ){
                        if( thisP.at( w + 2 ).at( h ) == thisS ){
                            return true;
                        }
                    }
                }catch( out_of_range& ){}

                try{
                    if( thisP.at( w - 1 ).at( h + 1 ) == thisS ){
                        if( thisP.at( w - 2 ).at( h + 2 ) == thisS ){
                            return true;
                        }
                    }
                }catch( out_of_range& ){}

                try{
                    if( thisP.at( w ).at( h + 1 ) == thisS ){
                        if( thisP.at( w ).at( h + 2 ) == thisS ){
                            return true;
                        }
                    }
                }catch( out_of_range& ){}

                try{
                    if( thisP.at( w + 1 ).at( h + 1 ) == thisS ){
                        if( thisP.at( w + 2 ).at( h + 2 ) == thisS ){
                            return true;
                        }
                    }
                }catch( out_of_range& ){}
            }
        }
    }
    ///
    /// One possibility
    ///
    for (int i = 0; i < thisP.size(); i++) {
        for (int j = 0; j < thisP.at(i).size(); j++) {
            if (thisP.at(i).at(j) == 0) {
                return false;
            }
        }
    }
    return true;
}

I did a little reading, and it looks like the fact that I am catching exceptions means that I am leaking memory, but I don't know how to resolve this. How can I refactor the code so I don't leak memory?

解决方案

It's important to understand the difference between leaking memory on a repeated basis (which can lead to exhaustion), and having some underlying support code or library have a one-off intialisation step that gets some heap memory it will use while the program runs (in which case it's not really useful or necessary to free/delete the memory at program termination, and it may be quite a hassle trying to arrange it).

Here, __cxa_get_globals seems to be doing a one-off malloc.

Short story: just make sure you don't get multiple unreleased blocks (or a bigger one) when those exceptions are called repeatedly....

相关文章