質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

480閲覧

メモリの解放部分に出るエラーの直しどころがわからない

NASIJIRU

総合スコア15

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2021/02/24 13:10

オセロを動かすコードで、状況判断をするcppファイルとして、以下のAI.cppがあり、コンパイルしたのですが、メモリ解放部分にエラーが出ます。

AI.cpp:40:3: warning: delete called on 'Evaluator' that is abstract but has non-virtual destructor [-Wdelete-abstract-non-virtual-dtor] delete Eval;

以下がAI.cppです。

#include "AI.h" #include "Board.h" #include "MidEvaluator.h" #include "BookManager.h" #include <limits> #include <algorithm> using namespace std; MinimaxAI::MinimaxAI() : AI() { } void MinimaxAI::move(Board& board) { BookManager book; vector<Point> movables = book.find(board); if(movables.empty()) { board.pass(); return; } if(movables.size() == 1) { board.move(movables[0]); return; } int limit; Eval = new MidEvaluator(); if(MAX_TURNS - board.getTurns() <= wld_depth) { delete Eval; limit = numeric_limits<int>::max(); if(MAX_TURNS - board.getTurns() <= perfect_depth) Eval = new PerfectEvaluator(); else Eval = new WLDEvaluator(); } else { limit = normal_depth; } int eval, eval_max = numeric_limits<int>::min(); Point p; for(unsigned i=0; i<movables.size(); i++) { board.move(movables[i]); eval = -negaMax(board, limit-1); board.undo(); if(eval > eval_max) p = movables[i]; } ☆ここでエラーが出てます!!!!!→delete Eval; Eval = NULL; board.move(p); } int MinimaxAI::negaMax(Board& board, int limit) { if(board.isGameOver() || limit == 0) { leaves++; return Eval->evaluate(board); } nodes++; const std::vector<Point>& movables = board.getMovablePos(); int score, score_max = std::numeric_limits<int>::min(); if(movables.empty()) { board.pass(); score = -negaMax(board, limit); board.undo(); return score; } for(unsigned i=0; i<movables.size(); i++) { board.move(movables[i]); score = -negaMax(board, limit-1); // ŽŸ‚Ì‘ŠŽè‚ÌŽè board.undo(); if(score > score_max) { score_max = score; } } return score_max; } AlphaBetaAI::AlphaBetaAI() : AI() { } void AlphaBetaAI::move(Board& board) { BookManager book; vector<Point> movables = book.find(board); if(movables.empty()) { board.pass(); return; } if(movables.size() == 1) { board.move(movables[0]); return; } int limit; Eval = new MidEvaluator(); sort(board, movables, presearch_depth); if(MAX_TURNS - board.getTurns() <= wld_depth) { delete Eval; limit = numeric_limits<int>::max(); if(MAX_TURNS - board.getTurns() <= perfect_depth) Eval = new PerfectEvaluator(); else Eval = new WLDEvaluator(); } else { limit = normal_depth; } int eval, alpha = numeric_limits<int>::min(); int beta = numeric_limits<int>::max(); Point p; for(unsigned i=0; i<movables.size(); i++) { board.move(movables[i]); eval = -alphabeta(board, limit-1, -beta, -alpha); board.undo(); if(eval > alpha) { alpha = eval; p = movables[i]; } }  delete Eval; Eval = NULL; board.move(p); } void AlphaBetaAI::sort(Board& board, std::vector<Point>& movables, int limit) { std::vector<Move> moves; for(unsigned i=0; i<movables.size(); i++) { int eval; board.move(movables[i]); eval = -alphabeta(board, limit-1, -INT_MAX, INT_MAX); board.undo(); Move move(movables[i].x, movables[i].y, eval); moves.push_back(move); } // •]‰¿’l‚Ì‘å‚«‚¢‡‚Ƀ\[ƒg std::stable_sort(moves.begin(), moves.end(), MoveGreater()); // Œ‹‰Ê‚̏‘‚«–ß‚µ movables.clear(); for(unsigned i=0; i<moves.size(); i++) { movables.push_back(moves[i]); } return; } int AlphaBetaAI::alphabeta(Board& board, int limit, int alpha, int beta) { if(board.isGameOver() || limit == 0) { leaves++; return Eval->evaluate(board); } nodes++; int score; const std::vector<Point>& movables = board.getMovablePos(); if(movables.empty()) { board.pass(); score = -alphabeta(board, limit, -beta, -alpha); board.undo(); return score; } for(unsigned i=0; i<movables.size(); i++) { board.move(movables[i]); score = -alphabeta(board, limit-1, -beta, -alpha); board.undo(); if(score >= beta) return score; alpha = std::max(alpha, score); } return alpha; } NegaScoutAI::NegaScoutAI() : AI() { } void NegaScoutAI::move(Board& board) { BookManager book; vector<Point> movables = book.find(board); if(movables.empty()) { // ‘Å‚Ä‚é‰ÓŠ‚ª‚È‚¯‚ê‚΃pƒX‚·‚é board.pass(); return; } if(movables.size() == 1) { // ‘Å‚Ä‚é‰ÓŠ‚ªˆêƒJŠ‚¾‚¯‚È‚ç’Tõ‚͍s‚킸A‘¦À‚É‘Å‚Á‚Ä•Ô‚é board.move(movables[0]); return; } int limit; Eval = new MidEvaluator(); sort(board, movables, presearch_depth); // Ž–‘O‚ÉŽè‚ð—Ç‚³‚»‚¤‚ȏ‡‚Ƀ\[ƒg if(MAX_TURNS - board.getTurns() <= wld_depth) { delete Eval; limit = numeric_limits<int>::max(); if(MAX_TURNS - board.getTurns() <= perfect_depth) Eval = new PerfectEvaluator(); else Eval = new WLDEvaluator(); } else { limit = normal_depth; } int eval, alpha = numeric_limits<int>::min(); int beta = numeric_limits<int>::max(); Point p; for(unsigned i=0; i<movables.size(); i++) { board.move(movables[i]); eval = -negaScout(board, limit-1, -beta, -alpha); board.undo(); if(eval > alpha) { alpha = eval; p = movables[i]; } } delete Eval; Eval = NULL; board.move(p); } void NegaScoutAI::sort(Board& board, std::vector<Point>& movables, int limit) { std::vector<Move> moves; for(unsigned i=0; i<movables.size(); i++) { int eval; board.move(movables[i]); eval = -negaScout(board, limit-1, -INT_MAX, INT_MAX); board.undo(); Move move(movables[i].x, movables[i].y, eval); moves.push_back(move); } // •]‰¿’l‚Ì‘å‚«‚¢‡‚Ƀ\[ƒg std::stable_sort(moves.begin(), moves.end(), MoveGreater()); // Œ‹‰Ê‚̏‘‚«–ß‚µ movables.clear(); for(unsigned i=0; i<moves.size(); i++) { movables.push_back(moves[i]); } return; } int NegaScoutAI::negaScout(Board& board, int limit, int alpha, int beta) { // [‚³§ŒÀ‚É’B‚µ‚½‚ç•]‰¿’l‚ð•Ô‚· if(board.isGameOver() || limit == 0) { leaves++; return Eval->evaluate(board); } nodes++; int a,b, t; a = alpha; b = beta; const std::vector<Point>& pos = board.getMovablePos(); if(pos.empty()) { // ƒpƒX board.pass(); t = -negaScout(board, limit, -beta, -alpha); board.undo(); return t; } for(unsigned i=0; i< pos.size(); i++) { board.move(pos[i]); t = -negaScout(board, limit-1, -b, -a); if(t > a && t < beta && i>0 && limit <= 2) { // Ä’Tõ a = - negaScout(board, limit-1, -beta, -t); } board.undo(); a = max(a, t); if(a >= beta) { // ƒÀŠ ‚è return a; } b = a+1; // V‚µ‚¢null window‚ðÝ’è } return a; }

このエラーは何を指しているのでしょうか?ググったところメモリリークに関係していると出ましたが、そもそもメモリリークしないように開放しているのにな。。。。と疑問に思っています。解答お願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

警告メッセージの通りです。なお、基本的にはダメな問題ですが、本当にそれで悪影響があるかは実装次第なので、これだけではなんとも言えません。( なのでエラーではなくて警告 )

問題は delete 文そのものではなくて、delete で指定されているポインタの指すクラスの設計です。
全体が見えないので推定ですが、MidEvaluator, PerfectEvaluator, WLDEvaluator と言ったクラスを共通の操作で使い分けるために、基底である Evaluator へのポインタ Eval で仮想メンバ関数を通して操作しているという状況がまずあって。

で、警告の 'Evaluator' that is abstract but has non-virtual destructor とある通り、仮想で使ってるのに「デストラクタはvirtualじゃない」という設計なので、それが問題だろうと指摘されているわけです。

なので、単純にはデストラクタをvirtualにすれば済む問題には見えますが。
なお、なんで「仮想で使ってるのにデストラクタがvirtualじゃない」のがマズいのかは、色々なところで説明されていると思いますので、検索していただければと。

投稿2021/02/24 14:22

angel_p_57

総合スコア1672

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

NASIJIRU

2021/02/25 01:20

ご回答ありがとうございます。ソースコードが分かりづらい貼り方で申し訳ございません。今からデストラクタがvirtualになるべきなのかについての話をググって、持っているロベールでも学びたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問