When can a memory leak occur?

前端 未结 8 2087
野性不改
野性不改 2021-02-08 06:52

I don\'t know what to think here...

We have a component that runs as a service. It runs perfectly well on my local machine, but on some other machine (on both machine RA

8条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-08 07:37

    One likely reason within your description is that you try to allocate a block of some unreasonably big size because of an error in your code. Something like this;

     size_t numberOfElements;//uninitialized
     if( .... ) {
        numberOfElements = obtain();
     }
     elements = new Element[numberOfElements];
    

    now if numberOfElements is left uninitialized it can contain some unreasonably big number and so you effectively try to allocate a block of say 3GB which the memory manager refuses to do.

    So it can be not that your program is short on memory, but that it tries to allocate more memory than it could possibly be allowed to under even the best condition.

提交回复
热议问题