bad alloc problem using mysql lib returning vectors of mysql_row
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: bad alloc problem using mysql lib returning vectors of mysql_row

  1. #1
    Join Date
    Mar 2010
    Posts
    1

    bad alloc problem using mysql lib returning vectors of mysql_row

    I'm trying to develop some kind of hockey simulator but I'm having this weird allocation memory error the second time it's getting called. I have about the same function for another query (but with players) and it works flawlessly. I'm trying to return my rows via a mysql_row vector and I'm not sure if it's a good idea. Since I don't really know how mysql_store_result works in the background and how vectors are bing handled I was wondering if anyone has an idea on how it works and what's the best solution for me... Here is my function

    vector<MYSQL_ROW> Database::get_goalers(string team){

    vector<MYSQL_ROW> goalers;
    vector<MYSQL_ROW>::iterator it;

    MYSQL_RES *result;
    MYSQL_ROW row;

    if (connection == NULL)
    cout << mysql_error(&mysql) << endl;
    else
    {

    string query = "SELECT * FROM goalies WHERE Team = '" + team + "' ORDER BY OV";

    mysql_query(connection, query.c_str()); //query the database
    result = mysql_store_result(connection);

    if (result == NULL)
    {
    cout << mysql_error(&mysql) << endl;
    }
    else
    {
    it = goalers.begin();
    while ((row = mysql_fetch_row(result)))
    {
    if (row != NULL)
    {
    //cout << row[0] << " and " << row[1] << endl ; //if I print the row before inserting the row is being printed correctly
    it = goalers.insert(it, row); //it crashes here (bad pointer) at the second time it's being called, third record.

    }
    }
    }
    //mysql_free_result(result); //Can't free at this moment
    }

    return goalers;
    }

    And here is the error I get...

    /*
    * If this ASSERT fails, a bad pointer has been passed in. It may be
    * totally bogus, or it may have been allocated from another heap.
    * The pointer MUST come from the 'local' heap.
    */
    _ASSERTE(_CrtIsValidHeapPointer(pUserData));


    If I use mysql_use_result it doesnt work and if I try to free the result after adding them to my vector the returned value gets erased... I guess the returned result is some kind of reference since I can't free the first result after storing it in my vector. What I need is to store those values somewhere in my ram during the whole game simulation, then after free it before simulating the next game. I sure could just send a vector of int containing the id of each goaler then make a select in each of the goalie constructor but it's not very optimized and I wish I won't have to change all of my classes structure because of this.

    Any idea?

  2. #2
    Join Date
    Mar 2010
    Posts
    1

    Re: bad alloc problem using mysql lib returning vectors of mysql_row

    Thinking of it I think I'll just return the pointer *result instead, it does the same job (instead of calling a for each to my vector I just use the mysql_fetch_row and it's about the same code...). Then I can free result after building my player object and I got no issue.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,446

    Re: bad alloc problem using mysql lib returning vectors of mysql_row

    What is a MYSQL_ROW?

    The std::vector class requires that the item that is being placed there is safely copyable and assignable. So you need to post the definition of MYSQYL_ROW before you can get any further help.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center