dotdotpan / exceptions4c

Automatically exported from code.google.com/p/exceptions4c
0 stars 0 forks source link

Memory leak by rethrow() #8

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Activate memory leak detection in Visual Studio: define _CRTDBG_MAP_ALLOC, 
include <crtdbg.h>, call _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | 
_CRTDBG_LEAK_CHECK_DF);

2. Create two nested try-catch blocks, on the inner try-block call throw(), on 
the inner catch-block call rethrow(), capture the exception  on the outer 
catch-block.

3. When the program finishes a 160 bytes memory leak is reported for memory 
allocated at the following statement in e4c.c, line 647: 

   cause = ( frame->thrown ? malloc( sizeof(*cause) ) : NULL );

4. If rethrow is called on the outer try-catch block, there is no leak.

What is the expected output? What do you see instead?

There should be no memory leak. The cause structure needs to be freed when no 
longer needed.

What version of the product are you using? On what operating system?
Exceptions4c 2.4.6
Microsoft Visual C++ 2010 Express
Windows 7

Please provide any additional information below.

Example code that triggers the error when run:
#ifdef _CRTDBG_MAP_ALLOC    /* MS Visual Studio malloc debug */
#include <stdlib.h>
#include <crtdbg.h>
#endif

#include <stdio.h>
#include "e4c.h"

void main (void)
{
    /* init memory leak detection */
#ifdef _CRTDBG_MAP_ALLOC    /* MS Visual Studio malloc debug */
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    /* send memory leak info also to STDERR */
    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR );
#endif

    e4c_context_begin(e4c_false, e4c_print_exception);
    try {
    void *buffer = malloc(1024);    /* leak 1KByte to make sure memory leak detection works */
    try {
        throw(NotEnoughMemoryException, "out of memory");
    }
    catch (RuntimeException) {
        rethrow("rethrowing");
    }
    }
    catch (RuntimeException) {
    printf("caught it\n");
    }

    e4c_context_end();
}

Result or running it:
caught it
Detected memory leaks!
Dumping objects ->
c:\data\projects\c\exceptions4c\testmemoryleak\testmemoryleak\e4c.c(647) : 
{132} normal block at 0x009C1FE8, 160 bytes long.
 Data: <  = out of memor> AC 88 3D 01 6F 75 74 20 6F 66 20 6D 65 6D 6F 72
c:\data\projects\c\exceptions4c\testmemoryleak\testmemoryleak\main.c(24) : 
{130} normal block at 0x009C1A90, 1024 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

Original issue reported on code.google.com by pauloscu...@gmail.com on 14 Aug 2011 at 4:05

GoogleCodeExporter commented 9 years ago
I'll handle this one. Thanks for reporting it :)

Original comment by guillermocalvo on 14 Aug 2011 at 6:59

GoogleCodeExporter commented 9 years ago
You're right; I've reproduced the error on my machine:

{{{
    Detected memory leaks!
    Dumping objects ->
    {58} normal block at 0x000D37B8, 160 bytes long.
     Data: < x  out of memor> AC 78 A3 00 6F 75 74 20 6F 66 20 6D 65 6D 6F 72 
    {56} normal block at 0x000D3240, 1024 bytes long.
    Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
    Object dump complete.
}}}

This bug was present in version 2.4.6 and it's already fixed in current 
development version (2.8.9). When I run again your example code, compiled along 
with the new version, the result is:

{{{
    Detected memory leaks!
    Dumping objects ->
    {56} normal block at 0x004F3240, 1024 bytes long.
     Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
    Object dump complete.
}}}

As you can see, the memory leak is currently solved. Please check it yourself 
on your platform. You can download the latest version of the library right from 
the repository. In order to compile your example code, you'll have to replace 
`e4c_false` to `E4C_FALSE`. No more changes will be needed.

The fact is that there have been major, internal, changes in the library from 
version 2.4.6 to the current one, but I haven't created another download file 
yet. I plan to package the library again and release it, when I get time to 
update the documentation properly. Meanwhile, I hope you don't mind using the 
source files on the repository: 
http://code.google.com/p/exceptions4c/source/browse/trunk#trunk%2Fsrc (sorry 
for the inconvenience).

Thank you very much for your report and please let me know if you find any 
other bugs.

Original comment by guillermocalvo on 15 Aug 2011 at 1:17

GoogleCodeExporter commented 9 years ago
Thanks for the quick reaction. The new version solved the problem.

Original comment by pauloscu...@gmail.com on 15 Aug 2011 at 5:07