LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2005, 08:33 PM   #1
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Rep: Reputation: 15
how to manage memory with an abnormal exit


Merry Christmas to everyone!

In my C code, when I allocate memory and if I get NULL for some reason, I'm supposed to leave with an error status (>= 1) and with eventually some message. But before to leave, I'd like to free previously allocated memory. This is simple in a simple code with 2-3 pointers. But in a complex program with a dozeon of multidimensional pointers this becomes tedious.

So, what strategy for memory management in abnormal exits do you adopt ?

Thanks
 
Old 12-24-2005, 09:04 PM   #2
devbro
Member
 
Registered: Jul 2005
Posts: 74

Rep: Reputation: 15
i can not remember what language it is but is there such thing as GC (garbage collector) in c or not? if there is don't read the rest of my post

one other thing you can do is to keep track of your pointers to allocated memeories using a link list. then write a function for exiting which shows a message, free that allocated memory and then return the exit status and finally exits the program.

if your program follows structure or function-base arcitecture then all you have to do is to pass the the link list to your exit function to free it for you.
 
Old 12-24-2005, 09:08 PM   #3
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Original Poster
Rep: Reputation: 15
GC is c++'s and java's ?

thanks for the linked list
 
Old 12-24-2005, 09:13 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You don't need a strategy for memory management upon exit: the OS releases any memory and closes any open files held by the process.

Java, C# and VB.Net are all Garbage-collected languages. C and C++ are not.

Both C and C++ allow you to substitute your own memory-handling functions (C by calling your own functions instead of "malloc()" and "free()"; C++ by overriding the "new" and "delete" operators). The primary motivation for doing so isn't necessarily "safety", but rather efficiency. It's a common technique in games programming or embedded systems, for example.

A simpler technique than the linked list is to:
1. Write a "wrapper" function for your own malloc() or free().

2. You call your wrapper, your wrapper calls malloc().

3. The wrapper requests a few extra bytes, and adds a "sentinel" or "canary" value
to the front and/or back of the data returned to you by malloc().

4. When you free the block, your wrapper first checks the "sentinel" values to make sure they weren't overwritten.
If they were (i.e. if memory were somehow corrupted), then your wrapper takes the appropriate action.
Logging a message and calling "abort()" is usually the Right Thing (no kidding!)

5. You'll recall that "free()" takes a pointer (void *) argument.
It would be better if your wrapper called a pointer to a pointer (void **), and then set your pointer to NULL after it called free(). That way, if any of your code tried to use the "dead" pointer, you'd find out REALLY fast!

'Hope that helps .. PSM

Last edited by paulsm4; 12-24-2005 at 09:24 PM.
 
Old 12-24-2005, 09:15 PM   #5
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Original Poster
Rep: Reputation: 15
euh... i agree about file pointers, but are you sure about memory pointers ?

Last edited by thelonius; 12-24-2005 at 09:21 PM.
 
Old 12-27-2005, 06:31 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you are using dynamic memory within the scope of a function, you need to delete it before the function returns in any case. If you have an exception that exits the function prematurely, your delete statement may be skipped.

If you are using dynamic memory in a class, you need to delete the memory in the destructor. If you keep both simple enough, the only things that would cause an exception in the constructor or destructor would be new/delete. Therefore; if you wrap the allocation and deallocation in a class and create an instance of the class on the stack (i.e. not with new) then when the program exits, the stack unwinds (even with an exception), thereby calling the destructor of your class, thereby freeing up your memory even on an exception.

If you are creating new objects (and not new[]), you can use std::auto_ptr (<memory>). If you need to use new[], then you will have to use your own class (or find one on the internet.)

I've used something like this before. This is off the top of my head, therefore it's a starting point rather than something you should use as-is:
Code:
template <class Type>
class AutoArray
{
public:
        AutoArray(unsigned int sSize = 1) : Data(NULL), DataSize(sSize)
        {
        if (DataSize) Data = new Type[DataSize];
        else          Data = new Type[1];
        }

        AutoArray(const AutoArray &eEqual) : Data(NULL), DataSize(eEqual.DataSize)
        {
        if (DataSize) Data = new Type[DataSize];
        else          Data = new Type[1];
        }

                AutoArray
        &operator = (const AutoArray&)
        { return *this; }

                Type
        &operator [] (unsigned int pPos)
        { return Data[pPos]; }

                const Type
        &operator [] (unsigned int pPos) const
        { return Data[pPos]; }

                unsigned int
        Size()
        { return DataSize; }

        ~AutoArray()
        { delete Data; }

private:
                unsigned int
        DataSize;

                Type
        *Data;
};
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
2.6.14 kernel + uClibc can't manage memory? Yerp Linux - Software 0 11-13-2005 05:51 PM
Abnormal traffic? xathras Linux - Security 4 05-08-2005 04:13 PM
Abnormal font sizes... Nazxul Linux - Software 3 02-06-2005 01:28 PM
installation exit abnormal !!!! meshmesh Linux - General 3 01-27-2004 12:22 PM
Mozilla abnormal exit msteph_hacker Linux - Software 6 11-01-2002 08:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration