• Advertisement

Archived

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

Strange memory leakage

This topic is 5901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there! Does anybody know why this code generates a memory leak, it seems fine to me.
        
typedef struct
{
	char* string;
} flens;

flens test = {"hello world\n"};


main()
{

	test.string = (char*)calloc(1, 4); // <- MEMORY LEAK 4 BYTES


	printf("\n\"%s\"\n", test.string);

return 0;
}
  
btw. I use
    
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
  
in MSVC6.0 to check for memory leaks. Thanks! /Fredrik Olsson Edited by - FXO on February 23, 2002 9:04:24 PM

Share this post


Link to post
Share on other sites
Advertisement
Add free(test.string) after your printf to release the memory.

Note that on the other hand, test.string initially points to a constant string ("hello world\n") which you must not free().

Share this post


Link to post
Share on other sites
The test.string is allocated in the lower-memory(i think), so it should not be freed.

When I try to free it i get an debug assertion failure:

    
/*
* If this ASSERT fails, a bad pointer has been passed in. It may be
* totally bogus, or it may have been allocated from another heap.
* The pointer MUST come from the 'local' heap.
*/




Edited by - FXO on February 23, 2002 10:05:31 PM

Share this post


Link to post
Share on other sites
well, you''ve substituted the pointer to "Hello World\n" with a pointer to a 4-byte array holding zeroes. The old pointer doesn''t matter since it is a constant. The new pointer must be freed or you get a memory leak.


main()
{
test.string = (char*)calloc(1, 4);
printf("\n\"%s\"\n", test.string);
free( test.string );
return 0;
}


This gives me no problem. (So question : have you applied the service packs to your compiler ?)

Share this post


Link to post
Share on other sites
I was just about to post my message, when I decided to try to restart MSVC, and then it worked, no errors no leaks!

I compiled/ compiled all, and it caused an debug assertion failure (instead of)/(before the) memory leak.

(Have anybody experienced anything similar?)

Anyways, thanks alot for your input, it solved the problem!
I was locked on thinking that the leak was caused at that line, since the output pointed at it and i overlooked anything else

Thanks
/Fide

Edited by - FXO on February 23, 2002 10:38:12 PM

Share this post


Link to post
Share on other sites
quote:
Original post by FXO
I compiled/ compiled all, and it caused an debug assertion failure (instead of)/(before the) memory leak.

(Have anybody experienced anything similar?)



I don''t know, I don''t check for memory leaks... I''m a Real ProgrammerTM. But VS sure is quirky. When in doubt, do a clean rebuild.

Share this post


Link to post
Share on other sites

  • Advertisement