[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Memory leak using "UnmanagedType.LPWStr"

news.mweb.co.za

3/15/2007 3:46:00 PM

Hi,

I assume I am doing something stupid but I can't see what it is.

I want to pass a structure containing a string from C# to C++.
It passes fine but has a memory leak.

I have the C# interop code below.
If I call Test() I can see a substantial leak even though FreeCoTaskMem()
is called.
If I make the string a constant size it goes away but I don't want to do
that since I will be using ::SysAllocString() in the C++ later.

Can anyone see what I am doing wrong?

Thanks, Vin

-------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
class MemoryLeakTester
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct Detail
{
[MarshalAs(UnmanagedType.LPWStr)]
public string m_source;
}
// store the pointer so I can free it
IntPtr m_detail;

private string m_source;

public MemoryLeakTester()
{
m_source = "testString";
}

public void Test()
{
for (int i = 0; i < 1000000; ++i)
{
IntPtr test = ToUnmanaged();
}
}

public IntPtr ToUnmanaged()
{
if (m_detail != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(m_detail);
m_detail = IntPtr.Zero;
}

Detail raw = new Detail();
raw.m_source = m_source;

int size = Marshal.SizeOf(raw);
m_detail = Marshal.AllocCoTaskMem(size);
Marshal.StructureToPtr(raw, m_detail, false);
return m_detail;
}
}
}


3 Answers

CSG

6/19/2008 7:49:00 AM

0

tHe_PC_JeLLy BeAn!! wrote:
> "zzpat" <zzpatrick@gmail.com> wrote in message
> news:g3c4jv3vic@enews2.newsguy.com...
>> Did Bush lie?
>>
>> He said the IAEA (the UN) wrote a report that said Saddam was six months
>> away from having nukes. The IAEA said Bush was lying, they had written
>> no such report. All the networks carried the claim unchallenged and few
>> if any corrected the lied.
>
> Since when does the media decide what wars to initiate?

That's not the point. The point is that virtually all mainstream media
outlets played a significant role in selling the war, as they always do.

> Of course he lied.

Which, of course, is what what zzpat just said.

Eddie Haskell

6/20/2008 4:53:00 PM

0


<tHe_PC_JeLLy BeAn!!> wrote in message news:1v900i.fhk.19.1@news.alt.net...
>
> "zzpat" <zzpatrick@gmail.com> wrote in message
> news:g3c4jv3vic@enews2.newsguy.com...
>> Did Bush lie?
>>
>> He said the IAEA (the UN) wrote a report that said Saddam was six months
>> away from having nukes. The IAEA said Bush was lying, they had written
>> no such report. All the networks carried the claim unchallenged and few
>> if any corrected the lied.
>
> Since when does the media decide what wars to initiate?
>
> Of course he lied.

So that means the Clintons lied too, right?

"The 1991 Persian Gulf War and subsequent U.N. inspections destroyed Iraq's
illicit weapons capability and, for the most part, Saddam Hussein did not
try to rebuild it, according to an extensive report by the chief U.S.
weapons inspector in Iraq that contradicts nearly every prewar assertion
made by top administration officials about Iraq."

http://www.washingtonpost.com/wp-dyn/articles/A12115-200...

"The community of nations may see more and more of the very kind of threat
Iraq poses now: a rogue state with weapons of mass destruction, ready to use
them or provide them to terrorists. If we fail to respond today, Saddam and
all those who would follow in his footsteps will be emboldened tomorrow."

-Bill Clinton - 1998

"In the four years since the inspectors left, intelligence reports show that
Saddam Hussein has worked to rebuild his chemical and biological weapons
stock, his missile delivery capability, and his nuclear program. He has also
given aid, comfort, and sanctuary to terrorists, including Al Qaeda members,
though there is apparently no evidence of his involvement in the terrible
events of September 11, 2001."

-Hillary Clinton - 2002

-Eddie Haskell


tHe_PC_JeLLyBeAn!!

6/21/2008 4:02:00 AM

0


"Eddie Haskell" <hheh@rrat.com> wrote in message
news:FdR6k.9358$PZ6.3737@bignews5.bellsouth.net...
>
> <tHe_PC_JeLLy BeAn!!> wrote in message
> news:1v900i.fhk.19.1@news.alt.net...
>>
>> "zzpat" <zzpatrick@gmail.com> wrote in message
>> news:g3c4jv3vic@enews2.newsguy.com...
>>> Did Bush lie?
>>>
>>> He said the IAEA (the UN) wrote a report that said Saddam was six months
>>> away from having nukes. The IAEA said Bush was lying, they had written
>>> no such report. All the networks carried the claim unchallenged and few
>>> if any corrected the lied.
>>
>> Since when does the media decide what wars to initiate?
>>
>> Of course he lied.
>
> So that means the Clintons lied too, right?

Clinton didnt invade Iraq based on a whopping great lie, you fucking
imbecile

No go back to sucking cock for $5 down on the docks.