Sign in to follow this  

Data allocation and error handeling

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

What happens to dynamically allocated data if the program terminates from a function error? Example:
int main()
{
  CClass* Object = new Class;
  
  if(!FailingFunction())
  {
    ErrorHandlerThatTerminatesProgram();
  }
  
  delete Object;
  
  return 0;
}



What happens to the allocated Object?

Share this post


Link to post
Share on other sites
It is leaked memory and is never deallocated. To prove this, you can use a memory manager. I used mmgr to verify this.

 
#include "stdafx.h"
#include "mmgr.h"

int main(int argc, char* argv[])
{
printf("Hello World!\n");

int * intarray = new int[1024];
return 0;
delete [] intarray;
return 0;
}



As you can see, the memory is never deallocated! The memory log is as follows:


1 memory leak found:

Alloc. Addr Size Addr Size BreakOn BreakOn
Number Reported Reported Actual Actual Unused Method Dealloc Realloc Allocated by
------ ---------- ---------- ---------- ---------- ---------- -------- ------- ------- ---------------------------------------------------
000003 0x0032B3D8 0x00001000 0x0032B3C8 0x00001020 0x00001000 new[] N N mem test.cpp(00011)::??





As you can see, important error handling is very important. Simply exiting out of a program with lots of memory allocated can be very disasterous if performed a lot of times - for the user runs out of avaliable memory.

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by Kaze
I dont think it would do anything, wont all the pages the program was alocated be returned once the program stops


This is a valid point. Really I do not know how windows will handle it - but it is not something I would be comfortable with leaving for windows to decide, you know? After allocating lots of memory (GB's) and not freeing it - everything seemed to have been taken care of on my computer when I exited a test program. The only thing that happened was that my comp got really sluggish afterwards and more memory allocations took longer.

- Drew

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, even Windows is able to free allocated memory after your app exits, memory will be freed, handles will be closed etc.

Share this post


Link to post
Share on other sites
all pages should be returned if a application ends since a program is given the exclusive use of a page, so wehn a program ends the os takes back the pages. But you defnitly want to close handles yourself, there a little diffrent, a handle is a number coresonding to a resorce thats stored outside the program(usally the OS) and since it not exclusive to any application(any app thats has that number can use, like HDC(0) on a windows machine(lets you paint to desktop)) it wont get taken back wen a app stops.

Share this post


Link to post
Share on other sites
If Windows doesn't free all it's allocated memory at program termination then I'd be surprised...it's a pretty easy task.
[edit] Beaten! (by half an hour? That's why it pays not to open threads before you're going to read them I guess...[smile]) Yeah, you're right Kaze, I forgot about handles and stuff and was transported back in time to last year (when I did an OS course where we implemented multiprogramming in NACHOS (a simple OS framework that sits on a MIPS simulator)).

[Edited by - lucky_monkey on February 11, 2005 4:20:24 AM]

Share this post


Link to post
Share on other sites

This topic is 4691 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this