When can a memory leak occur?

前端 未结 8 2086
野性不改
野性不改 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:30

    I fail to see why a stream would throw. Don't you have a dump of the failed process? Or perhaps attach a debugger to it to see what the allocator is trying to allocate?

    But if you did overload the operator <<, then perhaps your code does have a bug.

    Just my 2 (euro) cts...

    1. Fragmentation ?

    The memory could be fragmented.

    At one moment, you try to allocate SIZE bytes, but the allocator finds no contiguous chunk of SIZE bytes in memory, and then throw a bad_alloc.

    Note: This answer was written before I read this possibility was ruled out.

    2. signed vs. unsigned ?

    Another possibility would be the use of a signed value for the size to be allocated:

    char * p = new char[i] ;
    

    If the value of i is negative (e.g. -1), the cast into the unsigned integral size_t will make it go beyond what is available to the memory allocator.

    As the use of signed integral is quite common in user code, if only to be used as a negative value for an invalid value (e.g. -1 for a failed search), this is a possibility.

提交回复
热议问题