LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to manage memory with an abnormal exit (https://www.linuxquestions.org/questions/programming-9/how-to-manage-memory-with-an-abnormal-exit-396337/)

thelonius 12-24-2005 08:33 PM

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

devbro 12-24-2005 09:04 PM

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.

thelonius 12-24-2005 09:08 PM

GC is c++'s and java's ?

thanks for the linked list

paulsm4 12-24-2005 09:13 PM

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

thelonius 12-24-2005 09:15 PM

euh... i agree about file pointers, but are you sure about memory pointers ?

ta0kira 12-27-2005 06:31 AM

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;
};



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