两个会导致程序崩溃的注意点

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/NYWLLYD/article/details/48086913
1.接口变量为空的情况

    当一个文件需要使用另一个文件的接口变量时,如果这个接口变量为空的话会导致程序奔溃,因为访问了一个为nil的变量。这个地方比较隐蔽,因为是直接拿过来就用了,其实在大部分情况下访问nil的变量都会导致崩溃。
    因此,在对一个未知变量进行操作的时候最好要进行判断,因为这个变量可能别人给你传的就是nil。

2.NSMutableDictionary的setValue:forKey:和setObject:forKey:

    某次设置网络参数的时候,我用setObject:forKey:设置,发现每次程序运行到网络请求部分就崩溃,当时以为是网络请求完的结果处理不当导致的。经过断点发现每次运行到setObject:forKey:的时候就崩溃。于是发现自己平常使用setObject:forKey:和setValue:forKey:都是随机的,没有注意到这两个的区别。
    区别就是:
    1.setValue:forKey:中value能够为nil,为空的时候会调用removeObject:forKey:方法,其实就是等于没设置,但不会导致程序奔溃;但是,setObject:forKey:中value不能够为nil,不然会导致程序崩溃。
    2.setValue:forKey:中key只能传NSString类型,而setObject:forKey:的key可以任何类型
展开阅读全文

这样导致程序崩溃么?

08-16

_variant_t vData;//只定义了一个变量vData,后面对这个变量的使用(请问这会引起程序运行一段时间后崩溃么?)rnrn strSQLA70.Format(_T("SELECT NAMEOFAWARDS,ITEMOFAWARDS,WINNERSOFAWARDS,YEARORTIMEOFAWARDS,COPYRIGHTSTATEMENT,DURATION,STARTINGPOINT,PROGRAMTYPE,PROPERTITLE,SUBTITLEFORM,SYSTEM,INTRODUCTIONOFCONTENT FROM SBMA_SBCLIPDC WHERE OBJECTID=%ld"),lPackageID);rnrn pRsA70 = spDBA->GetRecordset((_bstr_t)strSQLA70);rn rnrn for (int j=0; jRecordCount; j++)rn rnrn vData = pRsA70->GetCollect(L"NAMEOFAWARDS");rn if(vData.vt != VT_NULL)rn rn rn pData->strAwardsName = (LPCTSTR)(_bstr_t)vData;rn rn rn vData = pRsA70->GetCollect(L"ITEMOFAWARDS");rn if(vData.vt != VT_NULL)rn rn rn pData->strAwards = (LPCTSTR)(_bstr_t)vData;rn rnrn vData = pRsA70->GetCollect(L"WINNERSOFAWARDS");rn if(vData.vt != VT_NULL)rn rn pData->strAwardsPs = (LPCTSTR)(_bstr_t)vData;rn rnrn vData = pRsA70->GetCollect(L"YEARORTIMEOFAWARDS");rn if(vData.vt != VT_NULL)rn rn pData->strAwardsYear = (LPCTSTR)(_bstr_t)vData;rn rn rn vData = pRsA70->GetCollect(L"COPYRIGHTSTATEMENT");rn if(vData.vt != VT_NULL)rn rn pData->strPgmCopyRight = (LPCTSTR)(_bstr_t)vData;rn rnrn vData = pRsA70->GetCollect(L"DURATION");rn if(vData.vt != VT_NULL)rn rn pData->strPgmTime = (LPCTSTR)(_bstr_t)vData;rn rnrn vData = pRsA70->GetCollect(L"STARTINGPOINT");rn if(vData.vt != VT_NULL)rn rn pData->strPgmIn = (LPCTSTR)(_bstr_t)vData;rn rnrn vData = pRsA70->GetCollect(L"PROGRAMTYPE");rn if(vData.vt != VT_NULL)rn rn pData->strPgmType = (LPCTSTR)(_bstr_t)vData;rn rnrn vData = pRsA70->GetCollect(L"PROPERTITLE");rn if(vData.vt != VT_NULL)rn rn pData->strPgmtTitle = (LPCTSTR)(_bstr_t)vData;rn 论坛

【疑难杂症】结构体定义导致程序崩溃

05-24

[code=C#]rn//类定义rn public class Classxxoorn rn private const int MAX_CLIPENUM_IN_BLOCK = 254; rn private const int BLOCK_INDEX_NUM = (8 * 1024); rn rn //[StructLayout(LayoutKind.Sequential)]rn private struct Block_Filern rn public uint frameNum; rn public uint clipStime; rn public uint clipEtime; rn public uint clip_size; rn public uint index_num; rn rnrn //[StructLayout(LayoutKind.Sequential)]rn private struct clipIdx_info_trn rn public byte channel;rn public byte status; rn public ushort fileInfoIdx; rn public Block_File blkfile; //注意,这一行注释掉就正常工作了rn public uint index_off; rn public uint clip_off; rn rnrn //[StructLayout(LayoutKind.Sequential)]rn private struct KeyIndexrn rn public uint cKeyOffset; rn public uint TimeStampSec; rn rnrn //[StructLayout(LayoutKind.Sequential)]rn private struct blockIdx_info_trn rn public uint block_inf; rn public uint block_size; rn public uint block_no; rn public byte channel; rn public byte block_status;rn public byte block_type; rn public byte clip_num; rn public uint recover_num; rn public uint write_Size; rn public uint blockStime; rn public uint blockEtime; rn public uint index_num; rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 28)]rn public byte[] rev;rn rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]rn public clipIdx_info_t[] clip_info;rn rn rnrn private blockIdx_info_t m_FileIndex;rnrn public Classxxoo( )rn rn m_FileIndex = new blockIdx_info_t();rn rn rnrnrn//------------------运行代码------------------------rnrn class Programrn rn static void Main(string[] args)rn rn Classxxoo xxoo = new Classxxoo( );rn Console.WriteLine(xxoo.ToString());rn Console.Read();rn rn rnrnrn[/code]rnrnrn异常提示rnrnrn未处理 System.TypeLoadExceptionrn Message=未能从程序集“ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“ConsoleApplication1.Classxxoo”。rn Source=ConsoleApplication1rn TypeName=ConsoleApplication1.Classxxoorn StackTrace:rn 在 ConsoleApplication1.Program.Main(String[] args)rn 在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)rn 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)rn 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()rn 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)rn 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)rn 在 System.Threading.ThreadHelper.ThreadStart()rn InnerException: rn 论坛

delete导致程序崩溃

10-27

我的程序大致架构是这样的rn[code=c]rn for (int i =0;im_csTagName = root[i]["TagName"].asCString();rn//...........中间是对item的操作,就不贴出了,rn rn然后是清理itemrn //清理item所占内存rn POSITION pos = Readitemlist.GetHeadPosition();rn while (pos)rn rn Item *item = Readitemlist.GetNext(pos);rn Readitemlist.RemoveHead();rn delete item;//崩溃地方,delete的时候item的内存地址是有的rn rn[/code]rn加了那个测试后,仍然崩溃,我猜测是Item类的定义导致的,但是我实在看不出问题,Item的头文件和构造函数如下rn[code=c]rn//Item的头文件rnclass Item:rn public CObjectrnrnpublic:rn Item();rn ~Item();rnrnpublic:rn CString m_csTagName;rn CString m_csTagAddress;rn CString m_csTagArea;rn CString m_csValue;rn CString description;rn int m_nTagAddress;rn int m_nBitAddres;//地址的位部分rn int m_nTagArea; //标签所属存储区,(十六进制),04:S 05:SM 06:AI 07:AQ 1E: C 81:I 82:Q 83:M 84:V 1F: Trn int m_nReadLength;//01:1 Bit 02:1 Byte 04:1 Word 06:Double Wordrn int m_nWriteLength;rn int DBNum; //DB块序号intrn int m_nsize;rn WORD quality;rn COleVariant value;rn SYSTEMTIME timestamp;rn;rn[/code]rn构造函数rn[code=c]rn GetLocalTime(&timestamp); //获取当前时间rn DBNum = 0;rn m_nTagAddress = 0;rn m_csTagName = "";rn m_csTagAddress = "";rn m_nTagArea = 0;rn m_nReadLength = 1;rn m_nWriteLength = 1;rn m_csTagArea = "";rn quality = 0;rn[/code]rn请大家帮忙看看 论坛

没有更多推荐了,返回首页