[comp.windows.x] Multiple GCs sharing fonts vs. XFreeGC

drk@twinkie.Rational.COM (David Kaelbling) (04/12/90)

I've run into problems sharing fonts in a widget I'm writing, and
wondered if anyone could straighten me out.  I'm running X.V11R4 with
fixes 1..9 installed on a Sun 3/80 under SunOS 4.0.3.  Everything is
compiled with gcc-1.35.

My widget has a font resource which supplies (via the usual toolkit
magic) a default XFontStruct*.  I create several GCs using this
default font->fid.  All goes smoothly until I attempt to exit.

When my destroy procedure calls XFreeGC on the second GC I created, it
dies with a BadFont error.  Xscope reveals that the first call to
XFreeGC issued a CloseFont request which released my font_id!

Having several GCs using the same font id doesn't seem like too
radical an idea (I don't see any way to reconstruct a font name for
another call XLoadFont), and I didn't see any red flags in the
documentation about common pitfalls.  My fid is a real font id, not a
context id in disquise.  Somebody else must have tried this.  Where
have I gone wrong?  (It's only obvious if you understand it?)

	Thanks,
	    David (drk@Rational.COM)

PS:  Why, oh why, doesn't the toolkit manage fonts and XFontStructs?
--
David Kaelbling                                       (408) 496-3600
c/o Rational; 3320 Scott Boulevard; Santa Clara, CA       95054-3197
Email: drk@Rational.COM, or uucp {uunet,ubvax,amdcad,aeras}!igor!drk

swick@ATHENA.MIT.EDU (Ralph R. Swick) (04/13/90)

    I'm running X.V11R4 with
    fixes 1..9 installed

     Xscope reveals that the first call to
    XFreeGC issued a CloseFont request which released my font_id!

There's something missing here.  XFreeGC does not cause a CloseFont
in the sources I'm looking at (nor should it!).

    Having several GCs using the same font id doesn't seem like too
    radical an idea

No, it's not at all radical -- in fact, it's quite normal.

    Somebody else must have tried this.

You bet.  Just to make sure, I've attached below some patches that
you may apply to examples/Xaw/xcommand.c and test the libraries you're
really using.

    PS:  Why, oh why, doesn't the toolkit manage fonts and XFontStructs?

More than the resource conversion cache does, you mean?  Maybe
you should describe what additional mechanisms you'd like.

	-Ralph.

--------
Test case:  apply the following to examples/Xaw/xcommand.c, making
a new file /tmp/gctest.c.  Each time you press the button, a new
GC will be created and destroyed using a single common font.  Also,
the button will be re-drawn using the same font id.

*** /x/examples/Xaw/xcommand.c	Fri Dec 15 19:37:16 1989
--- /tmp/gctest.c	Fri Apr 13 09:24:28 1990
***************
*** 88,94 ****
  Widget w;
  XtPointer call_data, client_data;
  {
!     fprintf(stdout, "Button Selected.\n");
  }
  
  /*	Function Name: Syntax
--- 88,102 ----
  Widget w;
  XtPointer call_data, client_data;
  {
!     GC gc;
!     XGCValues values;
!     XFontStruct *font;
! 
!     XtVaGetValues(w, XtNfont, (XtArgVal)&font, NULL);
!     values.font = font->fid;
!     gc = XCreateGC(XtDisplay(w), XtWindow(w), GCFont, &values);
!     printf("font id = %x; gc id = %x\n", values.font, gc->gid);
!     XFreeGC(XtDisplay(w),gc);
  }
  
  /*	Function Name: Syntax