473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

The strange behavior of this code

dh
If the put the code,

IntPtr ptr = Marshal.AllocCoTaskMem(4);
hr = em.Next(monikers.Length, monikers, ptr);
x = Marshal.ReadInt32(p);
// Use x below
....
....
Marshal.FreeCoTaskMem(p);

into a loop like this,

while(i < 5)
{
IntPtr ptr = Marshal.AllocCoTaskMem(4);
hr = em.Next(monikers.Length, monikers, ptr);
x = Marshal.ReadInt32(p);
// Use x below
...
...
Marshal.FreeCoTaskMem(p);
}

It would throw AccessViolation exception. Sometimes at AllocCoTaskMem();
other times at FreeCoTaskMem(). Any idea why that?

Thanks!
DH
Jul 31 '08 #1
1 1327
dh wrote:
If the put the code,

IntPtr ptr = Marshal.AllocCoTaskMem(4);
hr = em.Next(monikers.Length, monikers, ptr);
x = Marshal.ReadInt32(p);
// Use x below
...
...
Marshal.FreeCoTaskMem(p);

into a loop like this,

while(i < 5)
{
IntPtr ptr = Marshal.AllocCoTaskMem(4);
I don't see that you free that memory anywhere in the code...
hr = em.Next(monikers.Length, monikers, ptr);
x = Marshal.ReadInt32(p);
Where does the p variable come from?
// Use x below
...
...
Marshal.FreeCoTaskMem(p);
If you free the memory that p points to, and then try to access it in
the next iteration of the loop, that memory area may have been allocated
to another process. That would definitely cause an access violation.
}

It would throw AccessViolation exception. Sometimes at AllocCoTaskMem();
other times at FreeCoTaskMem(). Any idea why that?

Thanks!
DH


--
Göran Andersson
_____
http://www.guffa.com
Jul 31 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
12
by: John | last post by:
I can't get my head around this! I have the following code: <% .... Code for connection to the database ... .... Code for retrieving recordset ... If Not rs.EOF Then ... Do something...
19
by: david | last post by:
I took old code and decided to modify it a bit, and I just noticed that it does not compile at all and before server one of severs (main) crashed in the system it was working fine (I am really sure...
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.