C++;可变可视范围和应力 我是C++新手,不能理解某些行为。 有下面的功能,在这种情况下它工作 bool Network::doRequest(HTTPRequest& request, string path, string content) { HTTPResponse response; istream* respStreamPtr; session->sendRequest(request); respStreamPtr = &session->receiveResponse(response); if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED) { credentials->authenticate(request, response); session->sendRequest(request); respStreamPtr = &session->receiveResponse(response); } if (response.get("Content-Encoding") == "deflate") { Poco::InflatingInputStream inflater(*respStreamPtr); respStreamPtr = &std::istream(inflater.rdbuf()); Logger::dumpStream(*respStreamPtr); } return true; }

C++;可变可视范围和应力 我是C++新手,不能理解某些行为。 有下面的功能,在这种情况下它工作 bool Network::doRequest(HTTPRequest& request, string path, string content) { HTTPResponse response; istream* respStreamPtr; session->sendRequest(request); respStreamPtr = &session->receiveResponse(response); if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED) { credentials->authenticate(request, response); session->sendRequest(request); respStreamPtr = &session->receiveResponse(response); } if (response.get("Content-Encoding") == "deflate") { Poco::InflatingInputStream inflater(*respStreamPtr); respStreamPtr = &std::istream(inflater.rdbuf()); Logger::dumpStream(*respStreamPtr); } return true; },c++,pointers,inputstream,poco-libraries,C++,Pointers,Inputstream,Poco Libraries,但是如果移动字符串Logger::dumpStream(*respStreamPtr)超出if块。像这样: if (response.get("Content-Encoding") == "deflate") { Poco::InflatingInputStream inflater(*respStreamPtr); respStreamPtr = &std::istream(inflater.rdbuf()); } Logger::dumpStream(*r

但是如果移动字符串Logger::dumpStream(*respStreamPtr)超出if块。像这样:

  if (response.get("Content-Encoding") == "deflate") {
    Poco::InflatingInputStream inflater(*respStreamPtr);
    respStreamPtr = &std::istream(inflater.rdbuf());
  }
  Logger::dumpStream(*respStreamPtr);
停止工作!!! 条件(response.get(“内容编码”)=“deflate”)始终为真; 因此,在块中查看流内容会遇到麻烦。 但我不明白我做错了什么。 请帮帮我

在这两种情况下,都不例外。在第二种情况下,somefile.txt文件中没有数据。 在第一种情况下,文件somefile.txt已经膨胀了来自http请求的数据

void Logger::dumpStream(std::istream& inputStream) {  
  fstream outStream("somefile.txt", ios_base::trunc | ios_base::out | ios_base::binary);
  outStream << inputStream.rdbuf();
  outStream.close();
}
void记录器::转储流(std::istream&inputStream){
fstream扩展流(“somefile.txt”,ios_base::trunc | ios_base::out | ios_base::binary);

outStream我不熟悉您正在使用的类,但问题很可能是
Poco::InflatingInputStream inflater
超出了范围

在if语句中:

if (response.get("Content-Encoding") == "deflate") {
   Poco::InflatingInputStream inflater(*respStreamPtr);
   respStreamPtr = &std::istream(inflater.rdbuf());
} 

respStreamPtr
正被指向一个使用来自
充气机
对象的缓冲区的流。一旦if语句关闭,该缓冲区将不再有效,因此您无法在外部使用
respStreamPtr

我不熟悉您正在使用的类,但问题很可能是<代码>Poco::充气输入流充气器
超出范围

在if语句中:

if (response.get("Content-Encoding") == "deflate") {
   Poco::InflatingInputStream inflater(*respStreamPtr);
   respStreamPtr = &std::istream(inflater.rdbuf());
} 

respStreamPtr
被指向一个流,该流使用来自
充气机
对象的缓冲区。一旦if语句关闭,该缓冲区就不再有效,因此您不能在外部使用
respStreamPtr

指针本身实际上立即变为悬空。除VS之外的任何其他编译器都会禁用这是一个非标准代码。不要记临时人员的地址。谢谢你这么快的回答。请在主要问题中更新。添加新语句充气器=新。现在它工作了。但是现在我有一个内存泄漏,因为我没有删除充气对象?@greenif请不要编辑问题。问题已经回答。如果你有额外的问题问题,请写一篇新帖子。指针本身实际上马上就要挂起来了。除了VS以外的任何其他编译器都会禁止这种非标准代码。不要使用临时代码的地址。谢谢你这么快的回复。请在主要问题中更新。添加了new statement inflater=new。现在它可以工作了。但是现在我有了一个内存leaks,因为我没有删除充气对象?@greenif请不要编辑问题。问题已回答。如果您有其他问题,请发表新帖子。