From: Jon Mayo <jon.mayo@gmail.com>
To: Tiago Maluta <maluta_tiago@yahoo.com.br>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: swapcontext and free memory
Date: Thu, 19 Mar 2009 03:21:50 -0700 [thread overview]
Message-ID: <e694f9e00903190321y1c5280ebx26bb9d7a2dd7fae6@mail.gmail.com> (raw)
In-Reply-To: <49C149E5.4020309@yahoo.com.br>
On Wed, Mar 18, 2009 at 12:22 PM, Tiago Maluta
<maluta_tiago@yahoo.com.br> wrote:
> xcomp@arcor.de wrote:
>> Hi all,
>> I have written a small test case which simply creates a new context, swaps to it and returns to the main function. I am allocating memory for the ucontext_t structures and the stack on which the context is executed using malloc.
> Because of this I also want to deallocate the memory again using free.
> The problem is I can't deallocate the memory after swapping back. It
> runs only if all three free() calls are commented out. Otherwise a
> segmentation fault occurs. Does swapcontext handle this on its own? Can
> anyone explain this behavior? I didn't find anything concerning this
> problem in the web.
>> I am running this program on Ubuntu 8.04 with the following system information: Linux
> ubuntu8041 2.6.24-23-generic #1 SMP i686 GNU/Linux
>
> (...)
>
>> main_context = (ucontext*) malloc(sizeof(ucontext));
>> thread_context = (ucontext*) malloc(sizeof(ucontext));
>
> changing:
>
> main_context = (ucontext_t*) malloc(sizeof(ucontext_t));
> thread_context = (ucontext_t*) malloc(sizeof(ucontext_t));
>
> and compiling with free() I got:
>
> #./a.out
> start
>
> Allocated memory:
> main_context: 0x804b008
> thread_context: 0x804b168
> thread_stack: 0x804b2c8
> thread_function was called...
>
> swapcontext() returned. Freeing memory starts now...
> thread_stack was deallocated...
> thread_context was deallocated...
> main_context was deallocated...
>
> I'm using glibc version 2.8.
>
> --tm
>
>
>
I think you should be able to free() them fine. Could you post a short
bit of source that exhibits the problem? Also I tend to mmap() some
anonymous memory instead of using malloc() for things like makecontext
and sigaltstack. Because I want to put guard pages on my stacks due to
them generally being very small.
--
Jon Mayo
<jon.mayo@gmail.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
prev parent reply other threads:[~2009-03-19 10:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-03-18 18:05 swapcontext and free memory xcomp
2009-03-18 19:22 ` Tiago Maluta
2009-03-19 10:21 ` Jon Mayo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e694f9e00903190321y1c5280ebx26bb9d7a2dd7fae6@mail.gmail.com \
--to=jon.mayo@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
--cc=maluta_tiago@yahoo.com.br \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).