473,562 Members | 2,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The strange behavior of this code

dh
If the put the code,

IntPtr ptr = Marshal.AllocCo TaskMem(4);
hr = em.Next(moniker s.Length, monikers, ptr);
x = Marshal.ReadInt 32(p);
// Use x below
....
....
Marshal.FreeCoT askMem(p);

into a loop like this,

while(i < 5)
{
IntPtr ptr = Marshal.AllocCo TaskMem(4);
hr = em.Next(moniker s.Length, monikers, ptr);
x = Marshal.ReadInt 32(p);
// Use x below
...
...
Marshal.FreeCoT askMem(p);
}

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

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

IntPtr ptr = Marshal.AllocCo TaskMem(4);
hr = em.Next(moniker s.Length, monikers, ptr);
x = Marshal.ReadInt 32(p);
// Use x below
...
...
Marshal.FreeCoT askMem(p);

into a loop like this,

while(i < 5)
{
IntPtr ptr = Marshal.AllocCo TaskMem(4);
I don't see that you free that memory anywhere in the code...
hr = em.Next(moniker s.Length, monikers, ptr);
x = Marshal.ReadInt 32(p);
Where does the p variable come from?
// Use x below
...
...
Marshal.FreeCoT askMem(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
8508
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 of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
6
2254
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 the same method (aka delegate?). I use the Tag property within this method to determine what user action is taking place. Very simple: When...
12
20632
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
1732
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 that I remember that). Now I am getting this error: $ make g++ -Wall -ansi -pedantic -c pirma_lib.cpp g++ -Wall -ansi -pedantic -c pirma.cpp...
20
2206
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 ----------------------------------- #include <stdio.h> int main (void) { char xx="abcd"; char * p1 = xx;
0
7577
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7935
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6221
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5193
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3608
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2073
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
903
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.