Archived

This topic is now archived and is closed to further replies.

ProgrammerDave

malloc causing memory leaks?

Recommended Posts

I''m using Paul Nettle''s memory manager and it says that a malloc in my program is causing this leak. But I don''t seem to see what''s wrong with it. I''ve checked the heap with _heapchk() but there''s nothing wrong with it. Here''s some code. Actually this is from game programming gems 1, the text code in the appendix. One line is causing all the problems.
....
char *buffer;
....
buffer = (char *)malloc(MAX_STR);
 
Does anyone know why it would cause a memory leak? This is the manager output:
 ----------------------------------------------------------------------------------------------------------------------------------
|                                          Memory leak report for:  08/13/2003 17:20:35                                            |
 ---------------------------------------------------------------------------------------------------------------------------------- 


2 memory leaks found:

Alloc.   Addr       Size       Addr       Size                        BreakOn BreakOn              
Number Reported   Reported    Actual     Actual     Unused    Method  Dealloc Realloc Allocated by 
------ ---------- ---------- ---------- ---------- ---------- -------- ------- ------- --------------------------------------------------- 
000002 0x02D51048 0x00000004 0x02D50048 0x00002004 0x00000000 new         N       N    ??(00000)::??
000004 0x02D55B98 0x00001000 0x02D54B98 0x00003000 0x00000000 malloc      N       N    Text.cpp(00048)::??
 

Share this post


Link to post
Share on other sites
If you don''t know, a memory leak is where allocated memory is not de-allocated when it is finished with. So the malloc() is not causing the leak, the fact that the memory isn''t free()''d is most likely the problem.

You have a leak from new, too, which means an object created by "new" isn''t being deleted.

Sorry if you understood this already, it just sounds as though you might not''ve.

Share this post


Link to post
Share on other sites
That worked for some weird reason! The weird reason is that buffer was never allocated before that so I had no reason to call free(buffer)! Maybe some other function allocated it by mistake or something but now it''s fixed. Thanks anonymous poster.

Share this post


Link to post
Share on other sites
Does anyone know how I can find where the memory leak is caused for the first one? It''s in question marks and there''s no source line numbers or anything so is it maybe part of a dll or library I''m using which has no source? Or is there a way to track the leak through the addresses the manager gives me?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you want to find a memory leak, just tell the MSDev Studio to "break on Alloc," and supply the address of allocation. In your case it will be 0x02D51048. Look for this option in the debug menu.

Hope this helps!

Share this post


Link to post
Share on other sites
I would be inclined to put

buffer = (char *)malloc(sizeof MAX_STR); // Note that the sizeof function is used as an operator to return the size of the MAX_STR constant (?) in bytes.

Beginners and experts will find answers here.

Share this post


Link to post
Share on other sites
quote:

That worked for some weird reason! The weird reason is that buffer was never allocated before that so I had no reason to call free(buffer)! Maybe some other function allocated it by mistake or something but now it''s fixed. Thanks anonymous poster.


Of course you need to free it. You allocated the memory. You have to free it again. C does not have a garbage collector that does the cleanup for you. Every bit of memory that was allocated during runtime has to be free''ed before application shut down.

Share this post


Link to post
Share on other sites
No Wildfire! The problem was that the memory leak was caused at the start of the program when it started up, not when it shutdown. I said it was weird because I didn't even allocate it yet and it was causing some type of leak. Thanks for all your replies. I have fixed the error. The question marks came from me deleting an unallocated char pointer. And I have fixed the malloc problem also. Thanks.

[edited by - ProgrammerDave on August 14, 2003 3:57:24 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Agent1
That would just return the size of the type used to hold the MAX_STR constant, not the actual value in it.



-Agent1


That''s what I meant, Agent1.



Beginners and experts will find answers here.

Share this post


Link to post
Share on other sites
quote:
Original post by Mathematix
quote:
Original post by Agent1
That would just return the size of the type used to hold the MAX_STR constant, not the actual value in it.



That's what I meant, Agent1.


I don't understand. It's wrong to use sizeof(MAX_STR) if you want to allocate characters to hold that many characters. What you should do is use:

malloc(sizeof(char) * MAX_STR);




-Agent1

[edited by - Agent1 on August 15, 2003 1:47:25 PM]

Share this post


Link to post
Share on other sites