[C++] Segmentation fault bij het lezen van member variabele

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • cyberstalker
  • Registratie: September 2005
  • Niet online

cyberstalker

Eersteklas beunhaas

Topicstarter
Ik ben een beginner met C++ en ik zit met een segmentation fault waar ik niet uitkom. Ik heb de volgende header file:

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class group
{
    private:
        std::map<long, article *>           articles;           // articles in this group
        std::map<long, article *>::iterator article_iterator;   // iterator for the articles in this group
        class nntp                          *connection;        // usenet connection
        long                                low;                // low water mark in group
        long                                high;               // high water mark in group
    public:
        /**
          * Construct group based on low and high water mark
          *
          * @param  connection  connection to our usenet server
          * @param  low_mark    low water mark
          * @param  high_mark   high water mark
          */
        group(nntp *nntp_conn, long low_mark, long high_mark);

         /**
          * Fetch an article from the group
          *
          * @note   either headers or body has to be true
          *           otherwise the article will not be fetched
          *
          * @param  number      article number in group
          * @param  headers     prefetch headers
          * @param  body        prefetch body
          */
        article *fetch_article(long number, bool headers = true, bool body = true);
};


en de volgende functies

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// initialize the group with connection, low and high water mark
inline group::group(nntp *nntp_conn, long low_mark, long high_mark)
{
    // store connection and marks
    connection  =   nntp_conn;
    low         =   low_mark;
    high        =   high_mark;

    std::cout << "Group initialized with low: " << low << ", high: " << high << std::endl;
}

// fetch an article based on it's numeric id in this group
inline article *group::fetch_article(long number, bool headers, bool body)
{
    // if both headers and body are set to false, we have nothing to fetch
    if (!headers && !body)
        return NULL;

    std::cout << "Number: " << number << std::endl;
    std::cout << "Low: " << low << std::endl;
    std::cout << "High: " << high << std::endl;

    return NULL;
}


Ik krijg een segmentation fault in de fetch_article functie, op regel 20, waar ik de member variabele low probeer uit te lezen. Deze worden netjes gezet in de constructor, en de cout geeft ook een juiste waarde door.

Ik heb geen idee waarom dit gebeurt. Het zijn geen pointers, maar gewoon long int's. Ik kom er met google niet uit.

Ik ontken het bestaan van IE.


Acties:
  • 0 Henk 'm!

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 03:42

.oisyn

Moderator Devschuur®

Demotivational Speaker

de group waar je de fetch_object op aanroept is waarschijnlijk ongeldig (null pointer, al gedelete, nog nooit geallocate?). Kun je natuurlijk vrij gemakkelijk zien in de debugger door te kijken waar 'this' naar wijst.

[ Voor 28% gewijzigd door .oisyn op 14-12-2010 22:37 ]

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Acties:
  • 0 Henk 'm!

  • Nvidiot
  • Registratie: Mei 2003
  • Laatst online: 03-06 16:38

Nvidiot

notepad!

Als je er met een debugger niet uitkomt kun je ook nog even naar Valgrind kijken: http://valgrind.org/

What a caterpillar calls the end, the rest of the world calls a butterfly. (Lao-Tze)