Thread: Segfault problems

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    31

    Segfault problems

    I've been trying to write a wrapper to aid in win32 development. But for some reason I am getting segfaults when trying to access a class member.

    I have 2 classes, cAPP and cWIDGET.
    In cAPP I have a dynamic array of cWIDGET classes.

    I increment the array like this-
    Code:
    cWIDGET* tmp;
        this->nWidgets++;
        tmp = (cWIDGET*)realloc( this->widgets, sizeof( cWIDGET ) * this->nWidgets );
        this->widgets = tmp;
    But for some reason when I try to access a private member function from cWIDGET, GetHandle(), it segfaults when trying to access a private member variable, handle (of type HWND if it matters). I have no idea why it is segfaulting. I call another function Flush() before the GetHandle() function and it doesn't segfault when it sets handle to NULL..

    I figured it probably how I'm allocating the memory for the widget array. If not I can post more code, but I still have to narrow down where the problem really is.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > tmp = (cWIDGET*)realloc( this->widgets, sizeof( cWIDGET ) * this->nWidgets );

    It seems like you'd need to copy over your previous cWIDGET data from array this->widgets to tmp at this point. Otherwise it's gone by the next statement.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    31
    Well I think I've figured the problem out.. But it seems really weird!

    I passed a NULL pointer, which is supposed to be set as a parent node for the cWIDGET but it seems to have been trying to call a member function, like--

    (cWIDGET)NULL->handle

    I didn't think that was possible? How can a instance of the class exist with no allocated memory?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How can a instance of the class exist with no allocated memory?
    You've got the definition of a class done. You create a pointer to a class. Because the
    class is defined, we know what we should be able to use. Thus we can do:
    Code:
    ptrtoclass->classmemberfunction( );
    I don't know an easy way to word it so you'll understand it. You have to be able
    to do the above. Otherwise, what would you compile? Your program doesn't know if
    you've actually got anything real allocated at compile time. It's up to you to make sure
    that all of your pointers point at something. The code is there just to do a job. You
    tell it "When I give you a pointer to an object, access this member and do this that
    and the other thing."

    You have to make your pointer actually point at something, otherwise, you kill your
    program when you try and dereference it, or have some other BadThing™ happen.

    Quzah.
    Last edited by quzah; 09-12-2004 at 12:45 AM.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How did you allocate your first widget - did you use new or malloc/realloc ?
    Remember malloc will not call a constructor, and realloc will not call the constructor for any new items it allocates space for.

    Mixing new/delete and malloc/realloc/free is just wrong.

    How complex is the internal structure of a cWIDGET?
    If it contains internal pointers to other bits of itself (or other cWIDGETs), then bitwise copies of that memory to another location (which realloc will do from time to time) will break.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-02-2009, 07:24 AM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  4. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  5. string.assign -- SEGFAULT problems
    By nateDC in forum C++ Programming
    Replies: 5
    Last Post: 01-20-2005, 03:03 PM