Re: Still confused on new thread starting idle functions to update UI.





On Wed, Dec 4, 2013 at 2:59 PM, David Buchan <pdbuchan yahoo com> wrote:
   // Allocate memory on the heap, not stack.
   msgdata = (msgdatas *) malloc (1 * sizeof (msgdatas));
   msgdata->textview = (int *) malloc (1 * sizeof (int));
   message = (char *) malloc (1024);

The only blocks of memory that need to be on the heap are those that
will be passed around. These ones don't go anywhere, so they could be
automatic (ie on the stack).

If it helps, think of every block of memory as having an owner. When
you call strdup() or malloc() or anything like that, you get a pointer
to a block of memory which you now own. You can then pass that pointer
to another thread and abandon it, and the other thread now owns that
memory. Ultimately, the owner of the memory frees it, and then nobody
owns it.

ChrisA

================================
I agree "message" doesn't go anywhere, but doesn't the struct get passed?

What I mean is, is it kosher to have:

msgdatas msgdata;  // I'd pass &msgdata as arg. to g_idle_add() I suppose.

and then do the equivalent of the following:

msgdata->message = strdup (message);

I
 thought that would bomb, because the struct is on the stack while the 
char string within the struct is being malloc'd on the heap (strdup() uses malloc(), according to manpage).

I see John allocated memory for the struct with g_new() which I *think* uses malloc(). I believe I want to do 
it that way.

Dave


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]