Any idea why I have to call free() before Inline_Stack_Done? In order to
write some code sample documentation and advice, it would help to understand
why some things work only in a particular order.

In the Win32 code below, I have an long* cwp_Handles which is malloc'd and
free'd in the Child_Windows function but gets populated with child window
handles values via a callback (Child_Windows_Proc). After a little
experimentation, I figured out that it failed when the call to
free(cwp_Handles) followed Inline_Stack_Done, but when the two lines were
reversed it worked. Anyone know why?

thanks,

Garrett

P.S. C source for the example follows...

static long *cwp_Handles;
static int   cwp_idx_position;
static int   cwp_sz_x100;
BOOL FAR PASCAL Child_Windows_Proc (HWND hWnd, LPARAM Param) {
  long* tmp;

  cwp_Handles[cwp_idx_position++] = hWnd;
  if (cwp_idx_position%100 == 0) {
    tmp = realloc(cwp_Handles, (++cwp_sz_x100 * 100 * sizeof(long)));
    if (tmp != NULL) {
      cwp_Handles = tmp;
    } else {
      croak("Failed to realloc cwp_Handles");
    }
  }
  return 1;
}

void Child_Windows(long hWnd) {
  int i;
  Inline_Stack_Vars;

  cwp_idx_position = 0;
  cwp_sz_x100 = 1;
  cwp_Handles = malloc(cwp_sz_x100 * 100 * sizeof(long));

  if (hWnd) {
    EnumChildWindows(hWnd, *Child_Windows_Proc, 1);
  } else {
    EnumWindows(*Child_Windows_Proc, 1);
  }

  Inline_Stack_Reset;
  for (i=0; i<cwp_idx_position; i++) {
    Inline_Stack_Push(newSViv(cwp_Handles[i]));
  }
  free(cwp_Handles);
  Inline_Stack_Done;
}

Reply via email to