我在我继承的应用程序中有以下代码,使用VS2012针对boost 1.48.0构建
bool ConvertToBoolean(const std::string& s)
{
try
{
return boost::lexical_cast<bool>(s);
}
catch (...)
{
if (boost::iequals("true", s.c_str()))
{
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
如果将"True"或"False"传递给此方法,lexical_cast将抛出bad_lexical_cast异常,因为它期望"0"或"1"并将评估字符串比较.
这似乎在我的机器上运行良好,无论是在调试器内外(它不是总是?:)),但在我们的一个客户机器上异常以某种方式"泄漏"并在使用调试时导致以下消息转储文件:
application.exe_161117_152748.dmp中0x000007FEFD08A06D处的未处理异常:Microsoft C++异常:boost :: exception_detail :: clone_impl>在内存位置0x00000000002CD9B8.
堆栈跟踪:
KERNELBASE.dll!RaiseException() Unknown
snowagent.exe!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 154 C++
application.exe!boost::throw_exception<boost::bad_lexical_cast>(const boost::bad_lexical_cast & e) Line 61 C++
application.exe!boost::detail::lexical_cast_do_cast<bool,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >::lexical_cast_impl(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & arg) Line 1750 C++
application.exe!ConvertToBoolean(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & s) Line 111 C++
application.exe!CScanner::Exec() Line 326 C++
Run Code Online (Sandbox Code Playgroud)
什么可能导致这种泄漏?极为罕见的是你要责怪编译器,但由于VS2015中已经修复 …
我有一个看似简单的任务,这让我很头疼,希望得到一些帮助。
我想要完成的是将两个布尔值绑定到两个RadioButtons的 IsChecked 属性,共享相同的值GroupName(因此一次只检查一个)。
我面临的问题是,当 ContentPresenter 的内容即将更改时(通过绑定到 ComboBox 的 SelectedItem),当前内容会收到对具有相同属性值但来自视图的属性设置器的调用即将成为新内容的模型。(!) 最终结果是,尽管没有单击绑定到相关属性的 RadioButton,视图模型还是发生了变化。
我已经整理了一个显示问题的示例应用程序。要重现,请运行该应用程序并按照以下步骤操作:
如果在#3 和#4 之间,首先在组合框中选择“Two”,让ContentPresenter 显示另一个视图(通过DataTemplate 选择),则不会出现问题!?
有人可以解释为什么当 ContentPresenter 交换视图时在第 4 步设置属性,以及可以做些什么?
当INotifyPropertyChanged以最基本的形式实现接口时,大多数人似乎都像这样实现它::
public virtual void OnPropertyChanged(string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么要额外分配var propertyChanged = PropertyChanged;?这只是一个偏好的问题,还是有充分的理由呢?当然以下是有效的吗?
public virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下大量的XML数据:
<ArrayOfRESTDataSource xmlns="http://SwitchKing.Common/Entities/RESTSimplified/2010/07" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<RESTDataSource>
<Description>MyTest</Description>
<Enabled>true</Enabled>
</RESTDataSource>
</ArrayOfRESTDataSource>
Run Code Online (Sandbox Code Playgroud)
RESTDataSource 可以发生0-n次.
这是我的课程:
[DataContract( Namespace = "http://SwitchKing.Common/Entities/RESTSimplified/2010/07" )]
public class ArrayOfRESTDataSource
{
public RESTDataSource[] Data { set; get; }
}
[DataContract( Namespace = "http://SwitchKing.Common/Entities/RESTSimplified/2010/07" )]
public class RESTDataSource
{
[DataMember]
public bool Enabled { get; set; }
[DataMember]
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我从这样的服务器上读取了上面的XML数据:
WebRequest client = WebRequest.Create( "http://server:80/datasources" );
using( StreamReader sr = new StreamReader( client.GetResponse().GetResponseStream()) ) {
string xml = sr.ReadToEnd();
var response = ServiceStack.Text.XmlSerializer.DeserializeFromString<ArrayOfRESTDataSource>( xml …Run Code Online (Sandbox Code Playgroud) FreeRTOS队列周围的C++ wapper可以简化为:
template<typename T>
class Queue<T>
{
public:
bool push(const T& item)
{
return xQueueSendToBack(handle, &item, 0) == pdTRUE;
}
bool pop(T& target)
{
return xQueueReceive(handle, &target, 0) == pdTRUE;
}
private:
QueueHandle_t handle;
}
Run Code Online (Sandbox Code Playgroud)
xQueueSendToBack各州的文件:
The item is queued by copy, not by reference.
不幸的是,它实际上是通过副本,因为它都以a结尾memcpy,这是有道理的,因为它是一个C API.虽然这适用于普通旧数据,但是更复杂的项目(例如以下事件消息)会产生严重问题.
class ConnectionStatusEvent
{
public:
ConnectionStatusEvent() = default;
ConnectionStatusEvent(std::shared_ptr<ISocket> sock)
: sock(sock)
{
}
const std::shared_ptr<ISocket>& get_socket() const
{
return sock;
}
private:
const std::shared_ptr<ISocket> sock;
bool connected;
}; …Run Code Online (Sandbox Code Playgroud) c# ×3
.net ×1
c++ ×1
data-binding ×1
freertos ×1
servicestack ×1
shared-ptr ×1
visual-c++ ×1
wpf ×1