guile-user
[Top][All Lists]
Advanced

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

Application crashes 2


From: jblazi
Subject: Application crashes 2
Date: Sun, 1 Dec 2002 23:16:26 +0100

My application is very simple. It consists of a wxWindows text control where 
the user can input Guile commands and when a button is pushed, the text in 
the textcontrol is evaluated via scm_c_eval_str.

I initialize Guile with scm_init_guile() as I cannot control my main function.

Now as long as I enter Guile commands and no smob is included, everything is 
all right. But as soon as I create a smob, the application crashes.

Maybe there is a Good Soul out there who has pity with me and looks at my 
code? I removed more and more parts of it until it became very short and it 
still crashes.

So here you are:

struct punkt {
  double x,y;
  int symbol;
  int groesse,
      winkel;           //Nur 0 oder 45 werden auf dem Bildschirm unterschieden
  char text[12];        //Beschriftung
  char pos[4];
  int d;
  SCM update_func;
};

in a geometry.h file and then

#include <stdlib.h>
#include <libguile.h>

#include "wx/wx.h"
#include "CORE.h"
using namespace std;
using namespace CORE;

#include "zeichenblatt.h"
#include "geometry.h"

//================================== PUNKT 
=============================================
static scm_t_bits punkt_tag;

punkt* alloc_punkt()
{
  punkt* p = (punkt*) scm_must_malloc(sizeof(punkt),"punkt");
  p->update_func = SCM_BOOL_F;
  p->text[0]='\0';
  p->pos[0]='\0';
  return p;
}

bool gleiche_punkte(punkt& p,punkt& q)
{
  return p.x==q.x and p.y==q.y;
}

void zeichne(punkt* p,Zeichenblatt& zb,wxPaintDC& dc)
{
  #define r p->groesse

  float x=p->x.toFloat(),
        y=p->y.toFloat();

  wxCoord X=zb.x2p(x),
          Y=zb.y2p(y);

  dc.SetBrush(*wxBLACK_BRUSH);
  dc.DrawCircle(X,Y,r);
}

static SCM make_punkt(SCM x,SCM y)
{
  cout << "make-punkt " << endl;

  SCM_ASSERT(SCM_INUMP(x),x,SCM_ARG1,"make-punkt");
  SCM_ASSERT(SCM_INUMP(y),y,SCM_ARG2,"make-punkt");
  punkt *p = alloc_punkt();
  p->x = SCM_INUM(x);
  p->y = SCM_INUM(y);

  cout << "make-punkt " << p->x << "," << p->y << endl;

  SCM_RETURN_NEWSMOB(punkt_tag,p);
}

static SCM mark_punkt(SCM punkt_smob)
{
  return SCM_BOOL_F;
}

static size_t free_punkt (SCM punkt_smob)
{
  punkt *p = (punkt*)SCM_SMOB_DATA(punkt_smob);
  free(p);
  return (size_t)sizeof(punkt);
}

static SCM equalp_punkt(SCM punkt_smob1,SCM punkt_smob2)
{
  punkt *p1 = (punkt*)SCM_SMOB_DATA(punkt_smob1),
        *p2 = (punkt*)SCM_SMOB_DATA(punkt_smob2);

  return SCM_BOOL(gleiche_punkte(*p1,*p2));
}

static int print_punkt (SCM punkt_smob,SCM port,scm_print_state *pstate)
{
  scm_puts ("#<punkt>",port);
  return 1;
}

static SCM append_punkt(SCM punkt_smob)
{
  
SCM_ASSERT(SCM_SMOB_PREDICATE(punkt_tag,punkt_smob),punkt_smob,SCM_ARG1,"append-punkt");
  punkt *p = (punkt*)SCM_SMOB_DATA(punkt_smob);
  append(p);
  return SCM_UNSPECIFIED;
}

static SCM punkt_p(SCM arg)
{
  return SCM_BOOL(SCM_SMOB_PREDICATE(punkt_tag,arg));
}

void init_punkt_type (void)
{
  punkt_tag = scm_make_smob_type("punkt",sizeof(punkt));

  scm_set_smob_mark(punkt_tag,mark_punkt);
  scm_set_smob_free (punkt_tag,free_punkt);
  scm_set_smob_print (punkt_tag,print_punkt);
  scm_set_smob_equalp(punkt_tag,equalp_punkt);

  scm_c_define_gsubr("make-punkt",2,0,0,(SCM (*)())make_punkt);
  scm_c_define_gsubr("append-punkt",1,0,0,(SCM (*)())append_punkt);
  scm_c_define_gsubr("punkt?", 1, 0, 0,(SCM (*)())punkt_p);
}

void register_guile_functions()
{
  init_punkt_type();
}

//================================== APPEND 
======================================

struct obj_container {
  int typ;
  void *obj;
  };

struct obj_list {
  int n;
  obj_container *buf[2000];
  };

static obj_list liste;

void append(punkt *p)
{
     obj_container* oc = 
(obj_container*)scm_must_malloc(sizeof(obj_container),"container");
     punkt* pp=alloc_punkt();
     *pp = *p;
     oc->typ = 0;
     oc->obj = pp;
     liste.buf[liste.n++] = oc;
}


void zeichne_alle_objekte(Zeichenblatt &zb,wxPaintDC& dc)
{
  int i;

  punkt  *p;

  for(i=0;i<liste.n;i++) {
    if (liste.buf[i]->typ == 0) {
       p = (punkt*)(liste.buf[i]->obj);
       zeichne(p,zb,dc);
       free(p);
       free(liste.buf[i]);
       }
    }
  liste.n = 0;
}

void geo_init() 
{
  setDefaultInputDigits(CORE_INFTY);
  liste.n = 0;
}

It may be something ridiculous I am overlooking but maybe scm_init_guile() 
incorrectly determines the stack?

There are two scheme functions which are interpreted when the system is 
initialized:

(define (punkt x y . r)
    (make-punkt x y))

(define (zeichne . o-list)
  (let ((o (first o-list)))
    (append-punkt o)
    (if (> (length o-list) 1) (apply zeichne (cdr o-list)))))



TIA,
-- 
Janos Blazi





reply via email to

[Prev in Thread] Current Thread [Next in Thread]