Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..

C++
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
Posted
Updated 31-Oct-12 23:59pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Nov-12 2:10am    
This is called C++/CLI. If you change your tag ("Improve question", above), you will improve your chances for good answers.
--SA

1 solution

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
 
Share this answer
 
v3
Comments
Naveen_143 1-Nov-12 2:46am    
Thanks a lot..i ll try it
Sergey Alexandrovich Kryukov 1-Nov-12 2:59am    
You are welcome.
If you see it makes sense, please accept the answer formally (green button) -- thanks.
If you still have concerns, your follow-up questions are welcome.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900