MemoryLeakDetector: freeing memory that was malloced in a library function.

476 views
Skip to first unread message

Jan

unread,
May 4, 2012, 6:28:28 AM5/4/12
to cpputest
Hi all,

I'm using cpputest 3.1 on linux with gcc.

There is a scandir function which returns an array of structs
containing the files found in a certain directory. The memory for the
array of structs is allocated inside the scandir function but must be
free'ed in the program using the scandir func.

This leads to cpputests MemoryLeakWarningPlugin.cpp giving me a
(false) error about deallocation non-allocated memory.

Is there a way to avoid this problem?

Thanks in advance and best regards,
Jan

Terry Yin

unread,
May 4, 2012, 8:15:17 AM5/4/12
to cppu...@googlegroups.com
Hi Jan,

I have 2 potential solutions to the apparent problem. Passing CPPUTEST_USE_MEM_LEAK_DETECTION=N to your makefile will disable the memory leak detection. Another solution is in the UT version makefile for the library also add:
CXXFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h 
CFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h
 
But there are some smells of bad design, the problem you are with is exactly an example of why it's bad design.
Usually resource allocation & free should be symmetric, if you have a scandir library function allocating memory, you should have something like free_dir_array in the same library. And in the client code you always you them together.

br, Terry
--
-terry
-------------------------
Blog: http://terryyin.blogbus.com/
twitter: http://twitter.com/terryyin

Jan

unread,
May 4, 2012, 8:38:50 AM5/4/12
to cpputest
Hi Terry,

thanks for your quick response. Yeah it smells like bad design, but
since its in the official posix standard there is no way to improve
that function. (http://linux.die.net/man/3/scandir)
I think i will separate this particular test in a single test-
executable and go for the
CPPUTEST_USE_MEM_LEAK_DETECTION=N
approach in the makefile.

Best Regards, Jan

Terry Yin

unread,
May 4, 2012, 8:54:21 AM5/4/12
to cppu...@googlegroups.com
Hi Jan,

Oops, it's a standard function. Then what I said doesn't apply:P

Probably to stub/mock this function (or it's adaptor) is a better idea.

br, Terry

James Grenning

unread,
May 4, 2012, 9:41:01 AM5/4/12
to cppu...@googlegroups.com
Hello Jan

There is a macro called EXPECT_N_LEAKS, though I don't think you can set it to a negative number.

You could try this, though it does require an undesirable production code change

#ifdef free
#undef free
#endif

that would turn off cpputest_free for the rest of the source file. It's a kludge, but would work. Is this legacy code? If I was going todo this, I might create free_posix_hidden_stuff just for this purpose and limit the use to just where you need it.

It might be nice for something like IGNORE_UNKNOWN_FREE_CALLS in CppUTest that could be set on a per test basis.


James

Bas Vodde

unread,
May 5, 2012, 1:58:55 AM5/5/12
to cppu...@googlegroups.com

Hiya,

Though, all the answers will work :)

The way I'd to it is probably to use:

void PlatformSpecificFree(void* memory);
(you can find it in PlatformSpecificFunctions_c.h)

instead of normal free :) This is a thin wrapper around the free of the OS and it won't do any memory leak checking. That way you don't need to deal with #ifdef/#undef and also you can keep the memory leak checking on for everything else.

Hope that helps?

Thanks!

Bas

Jan

unread,
May 9, 2012, 2:21:34 AM5/9/12
to cpputest
Hi,

thanks for all your solutions. In the end i went for James' solution
creating a tiny helper lib containing only one mehtod:
PosixHelper_Free.
Maybe there will be more to add to this helper lib..

Btw I really like cpputest though I'm not that experienced in unit
testing.. And the community seems to be great to :)!

Have a nice day!

Jan

Bas Vodde

unread,
May 10, 2012, 10:48:55 PM5/10/12
to cppu...@googlegroups.com

Hi Jan,

Calling the PosixHelper_Free and calling the PlatformSpecificFree is actually the same thing :) The PlatformSpecificFree is already a small wrapper around free, so you can just call that and then you don't need the additional helper.

> Btw I really like cpputest though I'm not that experienced in unit
> testing.. And the community seems to be great to :)!

Thanks! I'm happy you find it useful.

Bas
Reply all
Reply to author
Forward
0 new messages