在c ++中返回一个局部变量导致析构函数冻结

时间:2017-03-25 06:24:01

标签: c++ memory allocation

这是示例代码

一级

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.LinearLayout;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import java.util.LinkedList;
import android.widget.GridLayout.LayoutParams;

import java.util.Arrays;


public class TestActivity extends AppCompatActivity {
public static int count = 0;
public static int x = 100;
//public static int[] xPos = new int[5];
public static int y = 100;
private boolean stopper = true;
public static int spot = 0;
//public static Button[] buttonArr = new Button[5];
public static LinkedList<Button> buttonList = new LinkedList<Button>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    int b = getIntent().getIntExtra("bob", 0);

    //System.out.println(b);
    if(b==-11978182){
        //System.out.println(buttonList.size());
        //for(int i = 0; i<buttonList.size(); i++) {
            //RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
            //Button addButton = new Button(this);
            //if(stopper) {
                //buttonList.add(addButton);
                //stopper = false;
            //}
            //System.out.println(buttonList.size());
            RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
            Button addButton = new Button(this);
            addButton.setId(spot);
            buttonList.add(addButton);
            System.out.println(buttonList.size());
            buttonList.get(spot).setText("add");
            RelativeLayout layout = new RelativeLayout(getApplicationContext());
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 200);
            layoutParams.setMargins(100*(spot+1), 100, 100, 100); // left, top, right, bottom
            buttonList.get(spot).setLayoutParams(layoutParams);
            //RelativeLayout layout = (RelativeLayout) findViewById(R.id.main);

            //for(int i = 0; i<buttonList.size(); i++) {
                mainLayout.addView(buttonList.get(spot));
                mainLayout.addView(layout);
            //}
            spot = spot + 1;
        //}
    }

    Button galleryButton = (Button) findViewById(R.id.gallery);
    galleryButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(TestActivity.this,Test2.class);
            startActivity(intent);
        }
    });


}
}

方法returnA()

class a {
public:
    int *i;
    a() {
        i = new int;
    }
    ~a(){
        delete i;
    }
};

然后我在main()

中调用了returnA
a returnA() {
    a A;
    A.i = new int;
    return A;
}

当代码运行到&#39;删除i&#39;时会抛出异常。在〜一个()中,当我将returnA()更改为堆上的allacote A时,问题消失了,i的区别是什么,没有一直在堆上分配,因为我使用new在构造函数中,请给我一些亮点

0 个答案:

没有答案