Re: [gtk-list] gpointer and callbacks



From: dross <dross@melbpc.org.au>
Subject: [gtk-list] gpointer and callbacks
Date: Wed, 14 Oct 1998 20:08:26 +0000

> 
> 
> I'm trying to write a simple audio interface, and am setting sliders to
> adjust the volume and mic levels. I have a callback function that I would
> like to pass an integer identifier, identifing the device I want to change
> the level for. The problem is I can't seem to pass an integer to this function.
> 
> I am new to gtk and I don't quite understand what a gpointer is, and in all the
> 
> examples I found only strings where used. Can you pass any type using gpointer?

gpointer is a generic pointer. You could cast it to whatever data
structure pointer you're pointing to.

You can pass data which are smaller than a pointer in the pointer
itself (potential portability problem warning):

void adjust_level (GtkAdjustment * adj, gpointer data)
{  
    int someint = (int)data;
...

void setup_window (GtkWidget * parent)
{
   int device
...
   hscale = gtk_hscale_new (GTK_ADJUSTMENT (vol_adj));
   gtk_signal_connect (GTK_OBJECT (vol_adj), "value_changed",
                       GTK_SIGNAL_FUNC (adjust_level), (gpointer)device);


Your code looks like a time-bomb since you pass a pointer to data on
the stack (device). When your setup_window function returns your stack
frame is likely to be overwritten. You should either declare "device"
as static (now your code is not reentrant) or allocate it dynamically,
but then you have to remember to deallocate, otherwise your
application might leak memory.

If you use a pointer to an int you could (using a static variable):

void adjust_level (GtkAdjustment * adj, gpointer data)
{  
    int device = *(int*)data;
...

void setup_window (GtkWidget * parent)
{
...
   static int device;

   device = ...

   gtk_signal_connect (GTK_OBJECT (mic_adj), "value_changed",
                       GTK_SIGNAL_FUNC (adjust_level), (gpointer)&device);


Examples are not compiled and might of course contain errors...

Petter
-- 
________________________________________________________________________
Petter Gustad     8'h2B | (~8'h2B) - Hamlet     http://home.sol.no/~pegu
#include <stdio.h>/* compile/run this program to get my email address */
int main(void) {printf ("pegu\100computer\056org\nmy opinions only\n");}





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