MS Visual Studio2010.C++ [英] MS Visual Studio 2010. C++

查看:79
本文介绍了MS Visual Studio2010.C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我只是在VS 2010中创建了一个Pinpad dll.它在一段时间内运行良好,但现在程序未运行.许多人怀疑可能存在内存泄漏.所以我试图调试它.该行显示超出范围的未处理异常索引的其他信息..

Hi all,
I just created one pinpad dll in VS 2010. It worked fine for sometime but now the program is not running. Many suspect there may be memory leak. SO i tried to debug it. It is showing additional information unhandled exception index out of range in the line..

System::Text::Encoding^ ascii = Encoding::ASCII;//setting the dest dest encoding format ie in* which to convert
System::Text::Encoding^ unicode = Encoding::Unicode;//setting from* which to convert
String^ unicStr = gcnew String(lpRandomParaH_3DES);
array<Byte>^ unicodeBytes = unicode->GetBytes(unicStr);

array<Byte>^ asciiBytes = Encoding::Convert( unicode, ascii, unicodeBytes );//converted from unicode byte to ascii byte

pin_ptr<Byte> p = &asciiBytes[0]; // here it is showing index out of range..

请帮助我解决它..提前感谢

Kindly help me out to resolve it..Thanks in advance

推荐答案

异常很简单:您遇到了asciiBytes具有长度的情况为零,因此索引0无效.有效范围为空,无法使用索引.您只需在显示的最后一行进行索引之前,检查asciiBytes的当前长度是否大于零即可.

显示的代码片段不会导致内存泄漏.当您执行gcnew时,将创建该对象,并且当该对象变为无法访问时,垃圾回收器(GC)将负责回收内存(更确切地说,是在稍后的某个时刻,您无法真正控制).请阅读以了解垃圾收集的基本概念以及可达性的作用:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ].

但是,不要以为不可能发生托管内存泄漏.我完全有可能,但这不是由于一些随机的错误所致,而是更典型地是由于代码的一般设计中的错误所致.我在过去的回答中对此事进行了解释.请参阅:
垃圾回收负责所有内存管理 [ ^ ],
在循环内部延迟变量会导致内存泄漏吗? [ ^ ],
摆脱的最佳方法导致内存不足的公共静态列表 [ MDI表单中的内存管理 [
The exception is simple: you came across the situations when asciiBytes has the length of zero, so the index 0 is invalid. The valid range is just empty, no index can be used. You simply should check that the current length of asciiBytes is greater than zero, before doing the indexing in the last line you show.

The fragment of code shown cannot cause the memory leaks. When you do gcnew, the object is created, and Garbage Collector (GC) takes care about reclaiming the memory, when the object becomes unreachable (more exactly, at some later moment of time which you cannot really control). Please read to learn the basic ideas of garbage collection and the role of reachability:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

However, don''t think that the leak of managed memory is not possible. I quite possible, but not as a result of some random bugs, but more typically due to mistakes in general design of the code. I explain this matter in my past answers. Please see:
Garbage collectotion takes care of all the memory management[^],
deferring varirable inside the loop can cuase memory leak?[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
Memory management in MDI forms[^].

Such memory leaks are much less likely, but if you have such kind of a leak, it''s harder to figure out. You will need to analyze your code design. I hope my explanations will help you to do that.

—SA


这篇关于MS Visual Studio2010.C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆