简体   繁体   English

R6025纯虚函数调用(从sf :: InputStream派生的类)

[英]R6025 pure virtual function call (class derived from sf::InputStream)

For my game, I want to use PhysFs to extract music files that are in a zip file 对于我的游戏,我想使用PhysF提取zip文件中的音乐文件

I created a custom class MusicStream that inherits from sf::InputStream that I use as an sf::Music 's stream. 我创建了一个自定义类MusicStream ,该类继承自sf::InputStream ,用作sf::Music的流。

This is my basic program: 这是我的基本程序:

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "musicstream.h"
#include "physfs.h"

int main() {
  PHYSFS_init(0);
  PHYSFS_addToSearchPath("data.zip", 0);

  std::string musicFile = "music.ogg";
  if (PHYSFS_exists(musicFile.c_str()) == 0) {
    PHYSFS_deinit();
    return EXIT_FAILURE;
  }

  sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
  sf::Music myMusic;
  MusicStream myStream(musicFile.c_str());
  if (!myStream.getError()) {
    myMusic.openFromStream(myStream);
    myMusic.play();
  }
  while (window.isOpen()) { 
    sf::Event event; 
    while (window.pollEvent(event)) { 
      if (event.type == sf::Event::Closed) window.close(); 
    }
  }

  myMusic.stop();

  PHYSFS_deinit();
  return 0;
}

This works flawlessly, except for one thing: 除以下几点外,这可以完美地工作:

When I close the window and the program exits, I'm getting a runtime error R6025 pure virtual function call and the program crashes. 关闭窗口并退出程序时,出现运行时错误R6025 pure virtual function call ,程序崩溃。

So apparently a pure virtual function is called ( sf::InputStream 's dtor??), but I implemented all of sf::InputStream 's functions and it doesn't make sense to me. 因此,显然一个纯虚函数被称为( sf::InputStream的dtor ??),但是我实现了sf::InputStream的所有函数,这对我来说没有意义。

Also, I'm not really sure if the code is relevant but in case it is, this is the custom class: 另外,我不确定代码是否相关,但如果是这样,这是自定义类:

musicstream.h 音乐流

#ifndef MUSIC_STREAM_H_INCLUDED
#define MUSIC_STREAM_H_INCLUDED

#include <SFML/System.hpp>
#include "physfs.h"

class MusicStream : public sf::InputStream {
 public:
  MusicStream();
  MusicStream(const char *fileName);
  virtual ~MusicStream() override;

  sf::Int64 read(void *data, sf::Int64) override;
  sf::Int64 seek(sf::Int64 position) override;
  sf::Int64 tell() override;
  sf::Int64 getSize() override;

  bool getError() const;

 private:
  PHYSFS_File *file_;
  bool error_;

};

#endif

musicstream.cpp musicstream.cpp

#include "musicstream.h"

MusicStream::MusicStream() :
  error_(true)
{
}

MusicStream::MusicStream(const char *filename) :
  error_(false)
{
  file_ = PHYSFS_openRead(filename);
  if (file_ == nullptr) {
    error_ = true;
  }
}

MusicStream::~MusicStream() {
  if (error_) { return; }
  PHYSFS_close(file_);
}

sf::Int64 MusicStream::read(void *data, sf::Int64 size) {
  if (error_) { return 0; }
  sf::Int64 fileRead = PHYSFS_read(file_, data, 1, size);
  if (fileRead == -1) {
    return 0;
  }
  return fileRead;
}

sf::Int64 MusicStream::seek(sf::Int64 position) {
  if (error_)  { return -1; }
  if (PHYSFS_seek(file_, position) == 0) {
    return -1;
  }
  return position;
}

sf::Int64 MusicStream::tell() {
  if (error_) { return -1; }
  sf::Int64 position = PHYSFS_tell(file_);
  return position;
}

sf::Int64 MusicStream::getSize() {
  if (error_) { return -1; }
  sf::Int64 size = PHYSFS_fileLength(file_);
  return size;
}

bool MusicStream::getError() const {
  return error_;
}

The problem were these two lines: 问题是这两行:

sf::Music myMusic;
MusicStream myStream(musicFile.c_str());

I swapped them and got rid of the error. 我交换了它们并摆脱了错误。 It's because music is played in its own thread. 这是因为音乐是在自己的线程中播放的。 It tried reading from the stream after it was destroyed. 在流被破坏后,它尝试从流中读取。 Now the music is destroyed before the stream is. 现在,音乐在流之前被销毁了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM