Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Memory leak depate. Linux vs windows vs another?

This topic is 4564 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

From what I know, in linux when a memory is allocated at the heap. It is allocated inside a page. The problem is, nothing checks if you read or write into memory outside of what you used malloc for, and it wont produce any page fault if you are still inside the allocated page. I heared that in windows there is some mechanisem to prevent this. I also heared that you can use vinegrind in linux or something like this. However, if you have the OS in windows detecting that leak, then you could probabblyt catch the leak with an exception, which allows your program to detect and deal with the leak. That means that you can detect the leak even in the end user's computer and then send a report about the leak to you, by the user. In linux the user would probabbly not use this vinegrind. So you windows have an advantahge here? Do you know how the leak detection works in windows? Or are there better ways to deal with memory leak in other OS? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
I think Windows does something to release memory that is leaked, i'm not sure how though. It shouldn't matter to you if you make sure your program doesn't contain memory leaks. Memory management is a solved problem, its not that difficult. Smart pointers should be all you need for most allocations. Its Valgrind by the way.

Share this post


Link to post
Share on other sites
Quote:
Original post by The C modest god
From what I know, in linux when a memory is allocated at the heap. It is allocated inside a page.
The problem is, nothing checks if you read or write into memory outside of what you used malloc for, and it wont produce any page fault if you are still inside the allocated page.
I heared that in windows there is some mechanisem to prevent this.
I also heared that you can use vinegrind in linux or something like this.
However, if you have the OS in windows detecting that leak, then you could probabblyt catch the leak with an exception, which allows your program to detect and deal with the leak.
That means that you can detect the leak even in the end user's computer and then send a report about the leak to you, by the user.
In linux the user would probabbly not use this vinegrind.
So you windows have an advantahge here?
Do you know how the leak detection works in windows?
Or are there better ways to deal with memory leak in other OS?

Thanks in advance.

No, most of that stuff you posted is wrong. Linux and Windows handle memory allocation more or less the same.

EDIT: For that matter, I'm not entirely sure that you know what a memory leak *is*. It has nothing to do with accessing unallocated memory. More or less the opposite, actually. You might want to read up more on memory leaks, and place less stock in what you've "heard".

[Edited by - Sneftel on January 21, 2006 4:41:17 PM]

Share this post


Link to post
Share on other sites
All modern operations systems completely release an application's allocated memory when it is destroyed.

So writing something like the code below isn't really that bad or a bug even.


int main() {
void* data = malloc(1024*1024*32);
// do something with 'data' here
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Libvan
All modern operations systems completely release an application's allocated memory when it is destroyed.

So writing something like the code below isn't really that bad or a bug even.

*** Source Snippet Removed ***

That's a dangerous piece of advice. Sure a modern OS will clean up the mess, but memory leaks become a problem when you have too many of them during execution, or on a platform that is less sophisticated than a PC running a modern OS (read: which might not clean up after you).


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by Libvan
All modern operations systems completely release an application's allocated memory when it is destroyed.

So writing something like the code below isn't really that bad or a bug even.


Yes it is!!! First off, it'll add cluter to sift through when you use a memory leak detector - which you'll probably do for any non-trivial project, if you're sane. Second off, if you're using the C++ equivilant new([]), not calling delete([]) will result in destructors not being called, and more complex resources not being freed correctly as a result (temporary files? named pipes? These should be cleaned up in something's destructor is you're using RAII).

This isn't even attacking the assumption that the OS will in fact clean up your process's memory - some older ones didn't, and I wouldn't be suprised if the same holds true for at least 1 arcane embeded platform - which for all you know that code will need to be ported to someday.

Just don't do it. Don't.

(Unless you're the writer of a platform-specific standard library, but even then, fie on you, bad monkey no biscut!!!).

Share this post


Link to post
Share on other sites
Well, if you write your own memory manager, you can detect leaks yourself.

Say you allocate 1000bytes in the heap, and 500 for a page. When you actually create that page, you REALLY allocate 504 bytes. Then those last four bytes you right WALL (or any 4 char word).

Than you can scan through your pages and if the last 4 bytes dont match, you know memory was overwritten. How often or when you want to scan is up to you. The scan shouldnt be that performance heavy since you can jump through your heap with pointer arithmatic and a quick memcmp will let you know if memory has been leaked.

hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by nef
Well, if you write your own memory manager, you can detect leaks yourself.

Say you allocate 1000bytes in the heap, and 500 for a page. When you actually create that page, you REALLY allocate 504 bytes. Then those last four bytes you right WALL (or any 4 char word).

Than you can scan through your pages and if the last 4 bytes dont match, you know memory was overwritten.

Again, that's not what a memory leak is.

Share this post


Link to post
Share on other sites
Ok, so it's not memory leak. So how is it called?
So does the OS in linux also checks if a program accessed memory inside a valid page but outside an array that was allocated with malloc?
Does windows do it as well?
Can you you catch with C exception the memory "glich" in windows? can you catch with a similar mechanisem the memory "glich" in linux with your problem.

I would appreaciate if you answer my question.
Thanks in advance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!