这真的返回本地地址吗?
我有一些代码创建了一个同步队列,我在数据收集类中使用它来报告其数据。创建队列的方法会发出警告:
Queue^% DataGatherer::AddOutputQueue()
{
Queue^ outputQueue = Queue::Synchronized(gcnew Queue);
AddOutputQueue(outputQueue);
return outputQueue;
}
1>.\DataGatherer.cpp(21) : 警告 C4172: 返回局部变量或临时变量的地址
这是我应该担心的警告还是在这种情况下我安全,这只是编译器对队列感到困惑: :Synchronized 返回Queue^
?代码似乎运行良好,但警告让我紧张;-)
I have some code which creates a synchronised queue which I use in a data gathering class to report it's data. The method which creates queues is kicking up a warning:
Queue^% DataGatherer::AddOutputQueue()
{
Queue^ outputQueue = Queue::Synchronized(gcnew Queue);
AddOutputQueue(outputQueue);
return outputQueue;
}
1>.\DataGatherer.cpp(21) : warning C4172: returning address of local variable or temporary
Is this a warning I should be worried about or am I safe in this case and it's just the compiler getting confused about Queue::Synchronized
returning a Queue^
? The code appears to run fine, but warnings make me nervous ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Queue^%
表示通过引用传递句柄。但是,func 内的句柄是一个局部变量,不能通过引用传递,因为当 func 完成时它可能会被销毁。从返回类型中删除%
就可以了。编辑:这并不意味着您的代码似乎可以工作。它随时都可以停止这样做。
Queue^%
indicates a handle being passed by reference. However, the handle inside the func is a local variable which can't be passed by reference since it's potentially destroyed when the func finishes. Remove the%
from the return type and you are fine.Edit: It doesn't mean anything your code seems to work. It can stop doing so any minute.