c++ - 尝试处理类的 vector 成员时程序崩溃

标签 c++ crash

您好,我已尽我所能来修复崩溃...但没有成功。所以我会尝试粘贴程序的主要组件,这样也许有人可以帮助解决这个问题......程序编译没有错误但它崩溃了。 如果我运行程序采用第一个选项“1”而不是输入...当它到达 save 部分时它就会崩溃。代码的某些部分被注释掉了,因为我试图找到崩溃的根源。如果我忘记了什么或者您需要其他详细信息,请询问 很抱歉,代码太长了。 主要功能及应用类:

#include "Catalog.h"
#include "UIconsole.h"
#include "StudentRepository.h"
#include "StudentValidator.h"


using namespace std;

class Application{
private:
    StudentRepository *stre;
    StudentValidator *stvl;
    Catalog *cat;
    UIconsole *ui;    
public:
    Application();
    ~Application();
    void run();
};    
int main(){
    Application app;
    app.run();
    return 0;
}    
Application::Application(){
    StudentRepository stre();
    StudentValidator stvl();
    Catalog cat();
    UIconsole ui();
}    
Application::~Application(){
}
void Application::run(){
    ui->runUI();
}

UIconsole.h(选择输入):

class UIconsole{
public:
    UIconsole(Catalog *catalog):catalog(catalog){};
    void runUI();

private:
    Catalog *catalog;

    void showMenu();
    int getOption();

    void addStudent();

界面控制台.cpp

int inp;
do{
    showMenu();
    inp = getOption();
    switch(inp){
    case 0: break;
    case 1: addStudent();break;
    }while(inp != 0);
}

void UIconsole::addStudent(){
    string name;
    int id,gr;
    cout<<"Name: ";
    cin>>name;
    cout<<"ID: ";
    cin>>id; cout<<endl;
    cout<<"Group: ";
    cin>>gr; cout<<endl;

    catalog->addNewStudent(name,id,gr);
//  try {
//      catalog->addNewStudent(n,id,gr);
//      cout<<"Student has been added!"<<endl;
//  } catch (Exception &ex) {
//      cout<<"Student HASN'T been added"<<ex.getMsg()<<endl;
//  }

}

目录.cpp:

void Catalog::addNewStudent(string name, int id, int gr){
Student st(name, id,gr);
//StudentValidator.
studRepo.save(st);

studentRepo.cpp:

void StudentRepository::save(Student A){
    string B;
    int check;
    B = A.getName();
//  check = findByName(B);
//
//  if(check != 0){
        students.push_back(Student(A));
//  }
//  else{
//      throw RepoException("Name already exist in the database!");
//  }
}

studentRepo.h

class StudentRepository{
private:
    vector <Student> students;
public:
    vector <Student> getAll();
    void save(Student);
    void edit(Student);
    void delByName(string);
    void searchById();

    int findByName(string name);

    ~StudentRepository();

};

跟踪:

    Dr. Memory version 1.4.6 build 2 built on Mar  7 2012 10:14:04
Application cmdline: ""D:\c++\Begin\L_6-8\Debug\L_6-8.exe""
Recorded 62 suppression(s) from default C:\Program Files (x86)\Dr. Memory/bin/suppress-default.txt

Error #1: UNINITIALIZED READ: reading register eax
# 0 _fu36___ZSt4cout                 [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:90]
# 1 UIconsole::runUI()               [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:19]
# 2 Application::run()               [D:\c++\Begin\L_6-8\Debug/../App.cpp:43]
# 3 main                             [D:\c++\Begin\L_6-8\Debug/../App.cpp:27]
Note: @0:00:10.842 in thread 5436
Note: instruction: mov    (%eax) -> %eax

Error #2: UNADDRESSABLE ACCESS: reading 0x00000004-0x00000008 4 byte(s)
# 0 std::vector<>::push_back()               [c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_vector.h:828]
# 1 StudentRepository::save()                [D:\c++\Begin\L_6-8\Debug/../StudentRepository.cpp:27]
# 2 Catalog::addNewStudent()                 [D:\c++\Begin\L_6-8\Debug/../Catalog.cpp:7]
# 3 _fu36___ZSt4cout                         [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:90]
# 4 UIconsole::runUI()                       [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:19]
# 5 Application::run()                       [D:\c++\Begin\L_6-8\Debug/../App.cpp:43]
# 6 main                                     [D:\c++\Begin\L_6-8\Debug/../App.cpp:27]
Note: @0:00:10.853 in thread 5436
Note: instruction: mov    0x04(%eax) -> %edx

Error #3: LEAK 21 direct bytes 0x006ff488-0x006ff49d + 0 indirect bytes
# 0 libstdc++-6.dll!Znwj           
# 1 libstdc++-6.dll!ZNSs7reserveEj 
# 2 libstdc++-6.dll!ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RSbIS4_S5_T1_E
# 3 _fu2___ZSt3cin                                                             [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:81]
# 4 UIconsole::runUI()                                                         [D:\c++\Begin\L_6-8\Debug/../UIconsole.cpp:19]
# 5 Application::run()                                                         [D:\c++\Begin\L_6-8\Debug/../App.cpp:43]
# 6 main                                                                       [D:\c++\Begin\L_6-8\Debug/../App.cpp:27]

DUPLICATE ERROR COUNTS:

SUPPRESSIONS USED:

ERRORS FOUND:
      1 unique,     1 total unaddressable access(es)
      1 unique,     1 total uninitialized access(es)
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total warning(s)
      1 unique,     1 total,     21 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
     84 still-reachable allocation(s)
         (re-run with "-show_reachable" for details)
Details: C:\Users\Warzaru\AppData\Roaming/Dr. Memory/DrMemory-L_6-8.exe.8092.000/results.txt

最佳答案

要注意的第一个问题是应用程序构造函数。 Application 类使用指针作为其成员,但您没有为这些指针分配数据。您想要将 Application 构造函数的主体替换为:

[成员(member)] = 新 [成员(member)类型];

这也可能会解决您的崩溃问题。

关于c++ - 尝试处理类的 vector 成员时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10601139/

相关文章:

c++ - Qt (qml) : Access Bundle file in Qt for Webview Url in ios?

c++ - 如何获取取消引用的 std::map 迭代器的类型?

c++ - 观察者设计模式及其他

iphone - iOS TableView 崩溃不知道如何。这是应用程序

ios - 如何在我的应用程序中添加 PLCrashReporter?

c++ - 在 Eigen (C++) 中求解欠定方程组

c++ - 在 dll 中使用映射时出现访问冲突错误

ios6 - 从 Crittercism 获得了这份崩溃报告。无法找出崩溃的原因。任何提示都会有所帮助

iphone - NSMutablearray removeallobjects 使我的应用程序崩溃。在 NSZombie 中运行良好

debugging - 是否可以将 gdb 附加到崩溃的进程(又名 "just-in-time"调试)