如果派生类仅包含自动变量成员,是否有必要使用虚拟析构函数?

时间:2019-05-25 12:53:50

标签: c++ oop memory

struct base
{
    base(){}
    ~base() { cout << "base destructor" << endl; }
};

struct derived : public base
{
    derived() : base() { vec.resize(200000000); }
    ~derived() { cout << "derived destructor" << endl; }
    vector<int> vec;
};

int main()
{
    base* ptr = new derived();
    delete ptr;

    while (true)
    {

    }
}

由于删除操作未调用派生对象的析构函数,因此以上代码泄漏。但是...

struct base
{
    base() {}
    ~base() { cout << "base destructor" << endl; }
};

struct derived : public base
{
    derived() : base() {}
    ~derived() { cout << "derived destructor" << endl; }
    int arr[200000000];
};

int main()
{
    base* ptr = new derived();
    delete ptr;

    while (true)
    {

    }
}

在第二种情况下,即使仅调用基本析构函数,内存也不会泄漏。所以我假设如果我的所有成员都是自动变量,那么没有基析构函数是安全的吗?当未调用派生对象的析构函数时,派生类中的“ arr”成员不会永不超出范围吗?幕后是怎么回事?

3 个答案:

答案 0 :(得分:3)

如果曾经通过指向该基类的指针删除了派生对象,则始终有必要在基类中具有虚拟析构函数。否则,该程序的行为是不确定的。在任何其他情况下,都不需要具有虚拟析构函数。班里有什么成员是无关紧要的。

答案 1 :(得分:3)

是的!

我看到您正在“切实地”考虑可能遗漏的破坏。考虑到派生类的析构函数不仅是您编写的析构函数体,在这种情况下,您还需要考虑成员破坏,并且您的建议可能无法破坏向量(因为例程非虚拟地破坏对象不会甚至知道有一个派生部分要考虑)。矢量已动态分配了将泄漏的内容。

但是,我们甚至不需要走那么远。程序的行为是不确定的,期限,故事的结尾。优化器可以根据您的代码有效进行假设。如果不是这样,您可以并且应该预期会发生奇怪的变化,这可能与您对计算机的预期工作方式不符。这是因为C ++是一种抽象,编译很复杂,并且您与该语言签有合同。

答案 2 :(得分:1)

没有必要发生内存泄漏并仍然调用UB。如果您的派生类不简单,则内存泄漏是一种预期的UB。示例:

package com.example.aufgabe2;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.maps.LocationSource;

public class MainActivity extends AppCompatActivity implements     LocationListener {

Button Open, locateme;
EditText latitude, longitude;

Double longitude_current;
Double latitude_current;
LocationManager locationManager;

String[] items_names = {"Munich", "Tunis", "Barcelone", "Frankfurt", "Wien", "paris", "london", "dubai"};
int[] Images = {R.drawable.munich, R.drawable.tunis, R.drawable.barcelona, R.drawable.frank, R.drawable.wien, R.drawable.paris, R.drawable.london, R.drawable.dubai};
String[] items_lati = {"48.13743", "36.8065", "41.3851", "50.110924", "48.210033", "48.8566", "51.5074", "25.2048"};
String[] items_lng = {"11.57549", "10.1815", "2.1734", "8.682127", "16.363449", "2.3522", "0.1278", "55.2708"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ListView listView = (ListView) findViewById(R.id.listView);
    CustomAdapter customAdapter = new CustomAdapter();
    listView.setAdapter(customAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            latitude.setText(items_lati[position]);
            longitude.setText(items_lng[position]);


        }
    });

    if (googleServiceAvailable()) {
        Toast.makeText(this, "PERFEKT ", Toast.LENGTH_LONG).show();
        init();
    }


    latitude = (EditText) findViewById(R.id.Latitude);
    longitude = (EditText) findViewById(R.id.Longitude);

    Open = (Button) findViewById(R.id.button);
    locateme = (Button) findViewById(R.id.button_locate_me);


    latitude.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    longitude.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

    //Open button for displaying the Map
    Open.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //condition here

            if (longitude.length() == 0) {
                longitude.setError("your longitude is empty");
            } else if (latitude.length() == 0) {
                latitude.setError("your latitude is empty");
            } else {
                Toast.makeText(MainActivity.this, "values added successfully ", Toast.LENGTH_SHORT).show();
                init();
            }

        }
    });

    // here is my locateme button 

    locateme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {

            }
            Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
            onLocationChanged(location);


        }
    });



}

public void init(){
    Open=(Button) findViewById(R.id.button);
    Open.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, MapsActivity.class);
            Double lati =Double.parseDouble(latitude.getText().toString());
            Double longi =Double.parseDouble(longitude.getText().toString());
             intent.putExtra("longitude",longi);
            intent.putExtra("latitude",lati);
            startActivity(intent);


        }
    });

}


  //testing the availability of google service
public boolean googleServiceAvailable(){
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int isavailable = api.isGooglePlayServicesAvailable(this);
    if (isavailable == ConnectionResult.SUCCESS){
        return true;
    }else if (api.isUserResolvableError(isavailable)){

        Dialog dialog = api.getErrorDialog(this,isavailable,0);
        dialog.show();
    }else{
        Toast.makeText(this,"Cant connect to play services ", Toast.LENGTH_LONG).show();
    }
    return false;

}

@Override
public void onLocationChanged(Location location) {
    Toast.makeText(this,"Cant connect your current location ", Toast.LENGTH_LONG).show();
    latitude_current=location.getLatitude();
    longitude_current=location.getLongitude();
    latitude.setText(String.valueOf(latitude_current));
    longitude.setText(String.valueOf(longitude_current));


}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}


class CustomAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return Images.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = getLayoutInflater().inflate(R.layout.mylist, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

        TextView textView_name = (TextView) view.findViewById(R.id.textView_name);


        imageView.setImageResource(Images[i]);
        textView_name.setText(items_names[i]);


        return view;
    }

}

C ++标准[expr.delete],第3段指出(2014年版)

  

在第一个替代方案(删除对象)中,如果   要删除的对象与其动态类型(静态)不同   type应该是要作为对象的动态类型的基类   删除,并且静态类型应具有虚拟析构函数或   行为是不确定的。在第二个替代方案(删除数组)中,如果   要删除对象的动态类型与静态类型不同,   行为是不确定的。

实际上,如果基类是微不足道的,那么所有字段都是微不足道的,而派生类不包含非静态或非平凡的成员,可能有人会争辩说,这些类是相等的,但是我还没有找到方法通过标准证明。很可能是IB而非UB。