使用VJSON在C++中解析FB好友列表,遇到内存问题,我该如何解决

Using a VJSON to parse a FB friends list in C++, running into memory issues, how would I fix this?

本文关键字:问题 何解决 解决 内存 遇到 C++ FB 使用 列表 好友 VJSON      更新时间:2023-10-16

我正在做一个项目,该项目以JSON格式获取你的Facebook好友列表,对其进行解析,并将其加载到特定的数据结构中。

在这种情况下,我将它放入一个arrayList中。我正在使用一个库,我们的教授在网上找到并建议我们使用,名为VJSON。我只是一名计算机工程专业的大二学生,所以那些图书馆里的大多数东西和班上的其他人一样,都有点让我不知所措。然而,他建议我们无论如何都要使用它。

VJSON类解析JSON文本文件并将其存储在javascript对象中。访问这些对象的方法是从"根"开始,然后以链表样式的格式转到下一个对象。

我在下面列出了我用来收集变量的方法。我们的教授还为我们提供了一切,直到for循环。正如你所看到的,在for循环的底部,我创建了一个人,并将其放入我的arrayList中,这很好。然而,在成功创建、存储和打印了大约14个人之后,它突然遇到了运行时错误。

我在xCode中运行了这个项目,下面是它给我的错误:

Project2(4244) malloc: *** error for object 0x100103c18: incorrect checksum for freed object - object was probably modified after being freed.
 *** set a breakpoint in malloc_error_break to debug

很明显,我可以看出这是某种内存管理问题,但除此之外没有什么问题。我不知道为什么会有任何物体被释放;如果它以某种方式被释放了,为什么其他人都没有被释放?此外,我试着用一个备用文本文件运行我的项目,使用一个朋友的朋友列表。在出现同样的错误之前,它设法通过了大约30人;为什么他的朋友比我的朋友多?

如有任何帮助,我们将不胜感激!这个VJSON的东西真的超出了我的想象。非常感谢。

arrayList createArrayList(string filename){
    arrayList people;
    ifstream fileInput(filename);
    //turn input stream into string
    // Black magic to turn input stream into string.  Remember to include streambuf header
    // This string constructor accepts an iterator to the start of the file input stream
    // and another one that represents the end of input character (which is supplied by calling the default constructor of the iterator).
    string inputStr((istreambuf_iterator<char>(fileInput)), istreambuf_iterator<char>());
    //parse file content into json object
    char *errorPos = 0;
    char *errorDesc = 0;
    int errorLine = 0;
    block_allocator allocator(1 << 10);
    json_value *root = json_parse(const_cast<char*>(inputStr.c_str()), &errorPos, &errorDesc, &errorLine, &allocator);
    //get the first element, data, this value is an array of JSON objects (people)
    json_value *list = root->first_child;
    //This outer loop addresses each person's JSON object
    for(json_value *it = list->first_child; it; it = it->next_sibling){
        //This inner loop looks at each key/value pair within each person object
        string first_name, last_name, birthday, hometownID, hometown, personIDstr;
        for(json_value *personKeys = it->first_child; personKeys; personKeys = personKeys->next_sibling){
            //If the key/value pair has a key of "first_name" collect value associated
            if(!string(personKeys->name).compare("first_name")){
                first_name = personKeys->string_value;

            }
            //collect last name
            else if(!string(personKeys->name).compare("last_name")){
                last_name = personKeys->string_value;
            }
            //collect birthday
            else if(!string(personKeys->name).compare("birthday")){
                birthday = personKeys->string_value;
                //trim bday to first five characters
                birthday = birthday.substr(0,5);
            }
            //collect hometown
            else if(!string(personKeys->name).compare("hometown")){
                for(json_value *homeKeys = personKeys->first_child; homeKeys; homeKeys = homeKeys->next_sibling){
                    if(!string(homeKeys->name).compare("id")){
                        hometownID = homeKeys->string_value;
                    }
                    if(!string(homeKeys->name).compare("name")){
                        hometown = homeKeys->string_value;
                    }
                }
            }
            else if(!string(personKeys->name).compare("id")){
                personIDstr = personKeys->string_value;
            }
            //create person object based on which values are included
        }
        Person person(birthday, first_name, last_name, hometown, 0);
        cout << person << endl;
        people.insert(people.size(), person);
        cout << people.get(people.size()-1) << endl;
    }
    return people;
}

来源https://code.google.com/p/vjson/

警告:vjson是破坏性解析器,即您的源缓冲区将被修改。

我认为这可能会在释放inputStr时导致错误。