Valgrind showing leaked memory with libobjc

261 Views Asked by At

Trying to track down what I'm missing here.

Running on CentOs5.

I have boiled this down to a simple simple program and still end up with a memory leak. Not sure if this is a bug in Valgrind or a bug in libobjc.

main.m

int main(){return 0;}

and I compile and link it with gcc

gcc main.m -o main -lobjc

When I run valgrind main I get leaked memory

==27005== HEAP SUMMARY:
==27005==     in use at exit: 8,485 bytes in 133 blocks
==27005==   total heap usage: 145 allocs, 12 frees, 11,053 bytes allocated
==27005==
==27005== LEAK SUMMARY:
==27005==    definitely lost: 16 bytes in 1 blocks
==27005==    indirectly lost: 16 bytes in 1 blocks
==27005==      possibly lost: 0 bytes in 0 blocks
==27005==    still reachable: 8,453 bytes in 131 blocks
==27005==         suppressed: 0 bytes in 0 blocks

If I don't link libobjc (leave off the -lobjc) I get no memory leaks.

Ideas?

2

There are 2 best solutions below

2
Mil0R3 On

Maybe there is not libobjc on your centOS? I build and run your code on Mountain Lion ,there is no warnings or errors.

0
joekarl On

Hmm... so it looks like memory is allocated for each Objective-C class that is declared. So linking libobjc ends up initializing some of these classes at run time whether you're using them or not. The class init cost seems to be a one time cost so I suppose that while memory isn't being released at the end of the program, it really isn't a leak that is going to hurt me because that memory has to be allocated for the entire length of the program anyways.

Still, kinda annoying though. Wish I could release that memory before the app quits.