我遇到了问题。当我启动我的程序时,它运行良好,但是当它继续时,它表示存在断点错误。然后它说内存分配存在问题,因此无法正常工作。这是代码: 部首:
#pragma once
#include <iostream>
#include <string>
class basics
{
private:
char *str;
int len;
static int string_obj;
public:
basics() { str = "ces", len = strlen(str), string_obj = 1; }
basics(const basics &st);
basics(const char *s);
~basics();
friend std::ostream & operator<<(std::ostream &os, const basics &st);
};
班级职能:
#include "basics.h"
int basics::string_obj = 0;
basics::basics(const char *s)
{
string_obj++;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
std::cout << string_obj << " objects was created." << std::endl;
}
basics::basics(const basics & st)
{
string_obj++;
len = std::strlen(st.str);
str = new char[len + 1];
std::strcpy(str, st.str);
std::cout << string_obj << " objects was created." << std::endl;
}
basics::~basics()
{
std::cout << string_obj << " objects was created. Now we'll delete one of them." << std::endl;
--string_obj;
std::cout << string_obj << " objects left." << std::endl;
delete[] str;
}
std::ostream & operator<<(std::ostream &os, const basics &st)
{
os << st.str;
return os;
}
致电部分:
#include "basics.h"
int main()
{
basics gigas("fear");
basics cop(gigas);
gigas.~basics();
system("pause");
return 0;
}
答案 0 :(得分:4)
不要明确调用析构函数:
public void remove_all_frags(){
android.support.v4.app.FragmentTransaction t = father.getSupportFragmentManager().beginTransaction();
List<Fragment> all_frags = father.getSupportFragmentManager().getFragments();
if (all_frags!=null && all_frags.size()>0){
for (Fragment a_frag:all_frags) {
if (a_frag!=null){
Log.i("Dialog Dates","removing-- "+a_frag.getTag()+ " - class-- " + a_frag.getContext().getClass().toString());
t.remove(a_frag);
}
}
t.commit();
}
}
当范围离开时,它将自动调用。因此出现崩溃,因为它实际上被调用了两次。