Thread: C++ Web server malloc memory error

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    C++ Web server malloc memory error

    Hi Everyone,
    So I am writing a very basic database in C++ and I am accessing the data from a web browser. I am using the opensource Mongoose web server code....

    I have an issue...
    The way the DB works is this:
    on starting, the DB loads a json file of all of the data into it. I have a class called DatabaseLoader that does this - it is the class that gets rewritten depending on the data structure of the json.

    This is passed to vectors (vector<Node*> and vector<Edge*>) as references from Graph object.

    Once the DatabaseLoader has finished it can be destroyed and any memory allocated objects it created (except the ones in those two vectors).
    From then on, the Graph object is in charge of all of the elements in the database that are stored in the two vectors.
    When the user browses to htpp://127.0.0.1:8000 they see the json representing each object in the vectors.

    All good so far....

    However, when I repeatedly hit refresh in my browser (and call me insane...) at quite a fast speed I get this error:

    Code:
    main(29855,0x7fff76763310) malloc: *** error for object 0x7f98b2829408: incorrect checksum for freed object - object was probably modified after being freed.
    *** set a breakpoint in malloc_error_break to debug
    [1]    29855 abort      ./main testing.json
    It seems to me this would be if I tried to "delete" and object twice, or if one of my objects was overwriting memory somewhere. However I am not recreating anything, I am just looping over the vectors and printing out the content.
    When I refresh slowly, I dont see this happen - i did it quite a lot of times, but when I do it fast I think it is happening.

    So is there any possibility of me hitting the c++ web server to quickly and it is trying to process the data twice, causing some sort of memory error - i.e do I need to implement threading or something??

    I can paste code, but there is quite a lot now....

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to create a smallest possible compilable example. Comment out code until the problem goes away and you've located the error. If you don't know how to solve that, check back here.
    Also consider using smart pointers if you aren't to remove risk of memory leaks and double frees.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. Replies: 6
    Last Post: 10-15-2012, 05:16 PM
  3. Memory allocation without malloc
    By messi89 in forum C Programming
    Replies: 5
    Last Post: 05-30-2010, 07:27 AM
  4. how to malloc memory?
    By zcrself in forum C Programming
    Replies: 11
    Last Post: 04-22-2010, 08:47 AM
  5. OS out of memory if malloc without free?
    By hkuser2001 in forum C Programming
    Replies: 7
    Last Post: 04-24-2006, 07:23 AM