Author Topic: very odd mudos segfault  (Read 3241 times)

Offline Nulvect

  • BFF
  • ***
  • Posts: 127
    • View Profile
very odd mudos segfault
« on: March 28, 2008, 07:10:22 pm »
In a piece of code I'm writing, I'm using an anonymous function like so:

Code: [Select]
// first populate the object array "allitems"
// bag is an object
allitems = filter(all_inventory(bag), (:
  (!($1->query_property("no steal")) && $1->get())
:) );

// I calculate the value of tmp here, shouldn't be important

allitems = sort_array(allitems,
  function (object a, object b, tmp) {
    int aw = a->query_weight();
    int bw = b->query_weight();
    mapping acv = a->query_curr_value();
    mapping bcv = b->query_curr_value();
    int agv = (acv && acv["gold"] ? acv["gold"] : 0);
    int bgv = (bcv && bcv["gold"] ? bcv["gold"] : 0);
    aw -= agv;
    bw -= bgv;
 
    aw -= bw;
    aw = aw + random(tmp) - random(tmp);

    if (aw < 0) return -1;
    else if (aw > 0) return 1;
    else return 0;
  },
tmp);

When I try to update this file, the mud segfaults. MudOS v22.2b14. Odd things: It doesn't matter whether the file successfully updates or not. It segfaulted both with syntax errors and without. Also, it sometimes takes a few heart_beats before it crashes.

I have gotten it to work by taking the anonymous function and making it a separate function, then calling it, like so:
Code: [Select]
int sortitems(object, object, int);



allitems = sort_array(allitems, (: "sortitems" :), tmp);


int sortitems(object a, object b, int tmp) {
  // exact same contents as the anonymous function posted above
}

Has anyone else had this sort of thing happen?? I use anonymous functions fairly often, does anyone have any tips on how to avoid this thing in the future??

Offline wodan

  • BFF
  • ***
  • Posts: 434
  • Drink and code, you know you want to!
    • View Profile
Re: very odd mudos segfault
« Reply #1 on: March 29, 2008, 08:48:45 am »
perhaps it's that you didn't give a type for tmp in the anonymous function, it could be something else though as in your non-crashing version you never call the function (: function name :) would but you did (: "function name" :) which will always return "function name", so you'll get some random sort result.

Offline Nulvect

  • BFF
  • ***
  • Posts: 127
    • View Profile
Re: very odd mudos segfault
« Reply #2 on: March 29, 2008, 01:51:39 pm »
Heh, that's what I get for retyping stuff in a hurry. tmp is an int in the bad code and I have no "" around the sortitems call in the working version.

Offline beek

  • Acquaintance
  • *
  • Posts: 2
    • View Profile
Re: very odd mudos segfault
« Reply #3 on: April 02, 2008, 09:10:22 pm »

IIRC, (: "x" :) is (: this_object, "x" :) and not (lambda () "x").
This is for compatibility with very old MudOS versions where function pointers were just (: object, string :) pairs.