#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0

    Question Freeing memory causes crash, needs editing but where?


    Hello, this is my first post and I'd like to say that reading the forums thus far has been a good learning experience. I have come across a problem of my own, after lurking for a while...

    However, after reading the READ THIS FIRST section, I am not sure how long is TOO long for a script/snippet, so before I post it and turn out to be just another obnoxious newbie, I'd like to ask how big is too big (it's 107 lines)? It does have a certain line where the problem seems to be coming from, however it is based on some prerequisite factors that isn't in the small bit of snippet it resides in.

    So anyway, I'll put in a small bit and if any more is needed I can accomodate. The purpose of this snippet is to free the object from the room and memory, which results in bus error.

    PHP Code:
    for(0MAX_WOUNDSx++)
            ((
    CHARACTER *)ev->owner)->wounds[x] = 0;
        
    send_character((CHARACTER *)ev->owner"Object released.\n\r");
        
    send_room((CHARACTER *)ev->ownerEXTRACT_CHARACTERNULLSEND_NOTCHARACTER"%s is gone, and releases %s.\n\r", ((OBJECT *)ev->target)->short_description, ((CHARACTER *)ev->owner)->name);
        
    object_from_room(((CHARACTER *)ev->owner)->corpse, ((CHARACTER *)ev->owner)->in_room);
        
    free_object(((CHARACTER *)ev->owner)->corpse);
        ((
    CHARACTER *)ev->owner)->corpse NULL;
        
    free(((CHARACTER *)ev->owner)->name);
        ((
    CHARACTER *)ev->owner)->name strdup(((CHARACTER *)ev->owner)->short_description); 
    The line that seems to be the problem is

    PHP Code:
    free_object(((CHARACTER *)ev->owner)->corpse); 
    Here's what the crashlog explains:

    Exception: EXC_BAD_ACCESS (0x0001)
    Codes: KERN_PROTECTION_FAILURE (0x0002) at 0x00000008

    Thread 0 Crashed:
    #0 0x00007000 in object_from_room (db.c:1774)
    #1 0x00009d14 in event_termpccorpsedecay (event.c:361)
    #2 0x0000e320 in main (main.c:187)
    #3 0x0000245c in _start (crt.c:267)
    #4 0x000022dc in start

    The operating system is the newest Darwin running gcc3. Please be gentle if I did something wrong. :o
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Without knowing the details of your objects, offhand I would say you should not be casting your call to free_object.

    You have this:

    free_object(((CHARACTER *)ev->owner)->corpse);

    Try this:

    free_object((ev->owner)->corpse);

    You need to be sure you are passing free_object an object it expects to be able to free.

    More would require detailed knowledge of the libraries you work with and should probably be directed to whatever group writes and maintains that code.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0
    Upon recompile I come across the warning:

    warning: dereferencing 'void *' pointer
    request for member 'corpse' in something not a structure or union

    Perhaps I should include the entire snippet? 107 lines :(
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    18
    The cast is ok. It isn't casting the value passed in to free_object. It's just used to dereference the pointer. Are you sure that corpse has been initialized?
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You can include the code, but without being able to dig down into the code for the routines you call, I don't know how valuable it will be.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0
    PHP Code:
    void event_add_pccorpsedecay(CHARACTER *chOBJECT *corpselong delaylong term)
    {
        
    EVENT *ev;

        
    ev new_event();
        
    ev->owner ch;
        
    ev->target corpse;
        
    ev->delay delay;
        
    ev->term term;
        
    ev->flags NOFLAG;
        
    ev->event_fptr event_pccorpsedecay;
        
    ev->term_fptr event_termpccorpsedecay;

        return;
    }

    void event_pccorpsedecay(EVENT *ev)
    {
        if(
    number_range(05) == 0)
            
    send_room((CHARACTER *)ev->ownerEXTRACT_CHARACTERNULLSEND_ALL"Object has been incapacitated.\n\r", ((OBJECT *)ev->target)->short_description);
        return;
    }

    void event_termpccorpsedecay(EVENT *ev)
    {
        
    OBJECT *obj;
        
    OBJECT *obj_next;
        
    int x;
        
    EVENT *aff;

        if((
    aff get_affect((CHARACTER *)ev->ownerEXTRACT_CHARACTERAFFECT_LIFE_WEB)) != NULL)
        {
            if(
    ev->count aff->delay)
            {
                
    ev->delay += aff->delay - (ev->count);
                
    send_character((CHARACTER *)ev->owner"A web encases the object.\n\r");
                return;
            }
            
    SET_BIT(aff->flagsEVENT_FREED);
        }

        for(
    obj = ((OBJECT *)ev->target)->containsobjobj obj_next)
        {
            
    obj_next obj->content_next;

            
    object_from_object(obj, (OBJECT *)ev->target);
            
    object_to_character(obj, (CHARACTER *)ev->owner);
        }
        for(
    0MAX_WOUNDSx++)
            ((
    CHARACTER *)ev->owner)->wounds[x] = 0;
        
    send_character((CHARACTER *)ev->owner"Object released.\n\r");
        
    send_room((CHARACTER *)ev->ownerEXTRACT_CHARACTERNULLSEND_NOTCHARACTER"%s is gone, and releases %s.\n\r", ((OBJECT *)ev->target)->short_description, ((CHARACTER *)ev->owner)->name);
        
    object_from_room(((CHARACTER *)ev->owner)->corpse, ((CHARACTER *)ev->owner)->in_room);
        
    free_object(((CHARACTER *)ev->owner)->corpse);
        ((
    CHARACTER *)ev->owner)->corpse NULL;
        
    free(((CHARACTER *)ev->owner)->name);
        ((
    CHARACTER *)ev->owner)->name strdup(((CHARACTER *)ev->owner)->short_description);

        
    SET_BIT(ev->flagsEVENT_FREED);
        
    event_add_recovery((CHARACTER *)ev->ownernumber_range((55 LOOPS_PER_SECOND), (65 LOOPS_PER_SECOND)));
        
    save_pc((CHARACTER *)ev->owner);
        return;



    I have been struggling with this for a while, and after some testing I have found that the code isn't pointing where the object and corpse originate correctly.

    For instance, last night I had two others helping me out. When I killed one of them, the game SHOWED that he was dead, however it didn't show that I was also dead. Program crash, d'oh. Same thing when two others fought, one died and the other was also shown as dead, but his corpse was not there.

    Thanks for the help so far, it's done a lot so far to help me understand what I did wrong.

IMN logo majestic logo threadwatch logo seochat tools logo