[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 with this code

Jeremy Hannon

2/26/2007 9:08:00 PM

I am not the greatest at COM Interop. I understand cleaning up your
resources, but usually I am using a VS generated interop library, or
primary interop assemblies. Making API Calls is somewhat new to me.

Could someone please explain to me why the code at
http://www.wischik.com/lu/Progr... would leak memory and how to
fix it. It is not my site, but we used it as an example.
5 Answers

Michael Phillips, Jr.

2/26/2007 9:35:00 PM

0

In the methods CopyTo1bb and SplashImage, the code uses SelectObject to
select the bitmap into a device context before using BitBlt.

However, the code does not select this bitmap out of the device context
after the BitBlt call. Resources are not freed properly.

You should use code such as the following:
IntPtr hOldBmp = SelectObject(hdc,hbm);
BitBlt(...);
SelectObject(hdc, hOldBmp);
....
Delete and or release DC's and other GDI objects


"Jeremy Hannon" <Jeremy.Hannon.NO_SPAM@AMR.NET> wrote in message
news:OVI5qoeWHHA.600@TK2MSFTNGP05.phx.gbl...
>I am not the greatest at COM Interop. I understand cleaning up your
>resources, but usually I am using a VS generated interop library, or
>primary interop assemblies. Making API Calls is somewhat new to me.
>
> Could someone please explain to me why the code at
> http://www.wischik.com/lu/Progr... would leak memory and how to fix
> it. It is not my site, but we used it as an example.


Jeremy Hannon

2/26/2007 10:47:00 PM

0

So, based on your message, I tried this in the CoyTo1bb sample:

IntPtr sdc = GetDC(IntPtr.Zero); // First we obtain the DC for the
screen
// Next, create a DC for the original hbitmap
IntPtr hdc = CreateCompatibleDC(sdc);
IntPtr tmpObj0 = SelectObject(hdc, hbm);
// and create a DC for the monochrome hbitmap
IntPtr hdc0 = CreateCompatibleDC(sdc);
IntPtr tmpObj1 = SelectObject(hdc0, hbm0);
// Now we can do the BitBlt:
BitBlt(hdc0, 0, 0, w, h, hdc, 0, 0, SRCCOPY);
SelectObject(hdc, tmpObj0);
SelectObject(hdc0, tmpObj1);

It still appears to be leaking memory, however. What did I miss?


Michael Phillips, Jr. wrote:
> In the methods CopyTo1bb and SplashImage, the code uses SelectObject to
> select the bitmap into a device context before using BitBlt.
>
> However, the code does not select this bitmap out of the device context
> after the BitBlt call. Resources are not freed properly.
>
> You should use code such as the following:
> IntPtr hOldBmp = SelectObject(hdc,hbm);
> BitBlt(...);
> SelectObject(hdc, hOldBmp);
> ...
> Delete and or release DC's and other GDI objects
>
>
> "Jeremy Hannon" <Jeremy.Hannon.NO_SPAM@AMR.NET> wrote in message
> news:OVI5qoeWHHA.600@TK2MSFTNGP05.phx.gbl...
>> I am not the greatest at COM Interop. I understand cleaning up your
>> resources, but usually I am using a VS generated interop library, or
>> primary interop assemblies. Making API Calls is somewhat new to me.
>>
>> Could someone please explain to me why the code at
>> http://www.wischik.com/lu/Progr... would leak memory and how to fix
>> it. It is not my site, but we used it as an example.
>
>

Michael Phillips, Jr.

2/26/2007 11:24:00 PM

0

You also need to update SplashImage.

You state there is a memory leak. What is your information source?

Are you getting a GDI resource leak or a memory leak?

I do not see anything else on the unmanaged side.

"Jeremy Hannon" <Jeremy.Hannon.NO_SPAM@AMR.NET> wrote in message
news:uFe2PgfWHHA.4624@TK2MSFTNGP03.phx.gbl...
> So, based on your message, I tried this in the CoyTo1bb sample:
>
> IntPtr sdc = GetDC(IntPtr.Zero); // First we obtain the DC for the
> screen
> // Next, create a DC for the original hbitmap
> IntPtr hdc = CreateCompatibleDC(sdc);
> IntPtr tmpObj0 = SelectObject(hdc, hbm);
> // and create a DC for the monochrome hbitmap
> IntPtr hdc0 = CreateCompatibleDC(sdc);
> IntPtr tmpObj1 = SelectObject(hdc0, hbm0);
> // Now we can do the BitBlt:
> BitBlt(hdc0, 0, 0, w, h, hdc, 0, 0, SRCCOPY);
> SelectObject(hdc, tmpObj0);
> SelectObject(hdc0, tmpObj1);
>
> It still appears to be leaking memory, however. What did I miss?
>
>
> Michael Phillips, Jr. wrote:
>> In the methods CopyTo1bb and SplashImage, the code uses SelectObject to
>> select the bitmap into a device context before using BitBlt.
>>
>> However, the code does not select this bitmap out of the device context
>> after the BitBlt call. Resources are not freed properly.
>>
>> You should use code such as the following:
>> IntPtr hOldBmp = SelectObject(hdc,hbm);
>> BitBlt(...);
>> SelectObject(hdc, hOldBmp);
>> ...
>> Delete and or release DC's and other GDI objects
>>
>>
>> "Jeremy Hannon" <Jeremy.Hannon.NO_SPAM@AMR.NET> wrote in message
>> news:OVI5qoeWHHA.600@TK2MSFTNGP05.phx.gbl...
>>> I am not the greatest at COM Interop. I understand cleaning up your
>>> resources, but usually I am using a VS generated interop library, or
>>> primary interop assemblies. Making API Calls is somewhat new to me.
>>>
>>> Could someone please explain to me why the code at
>>> http://www.wischik.com/lu/Progr... would leak memory and how to
>>> fix it. It is not my site, but we used it as an example.
>>

Jeremy Hannon

2/27/2007 2:05:00 AM

0

Nevermind. We tracked down the main culprit and it wasn't this code at
all. Actually, it appears to be a leak in the ghostscript API. They
have a patch checked it that we are going to try.

Thank you very much for you help, though. We still made those changes
to be on the safe side, and we will keep an eye on it.

Again, thank you.

Michael Phillips, Jr. wrote:
> You also need to update SplashImage.
>
> You state there is a memory leak. What is your information source?
>
> Are you getting a GDI resource leak or a memory leak?
>
> I do not see anything else on the unmanaged side.
>
> "Jeremy Hannon" <Jeremy.Hannon.NO_SPAM@AMR.NET> wrote in message
> news:uFe2PgfWHHA.4624@TK2MSFTNGP03.phx.gbl...
>> So, based on your message, I tried this in the CoyTo1bb sample:
>>
>> IntPtr sdc = GetDC(IntPtr.Zero); // First we obtain the DC for the
>> screen
>> // Next, create a DC for the original hbitmap
>> IntPtr hdc = CreateCompatibleDC(sdc);
>> IntPtr tmpObj0 = SelectObject(hdc, hbm);
>> // and create a DC for the monochrome hbitmap
>> IntPtr hdc0 = CreateCompatibleDC(sdc);
>> IntPtr tmpObj1 = SelectObject(hdc0, hbm0);
>> // Now we can do the BitBlt:
>> BitBlt(hdc0, 0, 0, w, h, hdc, 0, 0, SRCCOPY);
>> SelectObject(hdc, tmpObj0);
>> SelectObject(hdc0, tmpObj1);
>>
>> It still appears to be leaking memory, however. What did I miss?
>>
>>
>> Michael Phillips, Jr. wrote:
>>> In the methods CopyTo1bb and SplashImage, the code uses SelectObject to
>>> select the bitmap into a device context before using BitBlt.
>>>
>>> However, the code does not select this bitmap out of the device context
>>> after the BitBlt call. Resources are not freed properly.
>>>
>>> You should use code such as the following:
>>> IntPtr hOldBmp = SelectObject(hdc,hbm);
>>> BitBlt(...);
>>> SelectObject(hdc, hOldBmp);
>>> ...
>>> Delete and or release DC's and other GDI objects
>>>
>>>
>>> "Jeremy Hannon" <Jeremy.Hannon.NO_SPAM@AMR.NET> wrote in message
>>> news:OVI5qoeWHHA.600@TK2MSFTNGP05.phx.gbl...
>>>> I am not the greatest at COM Interop. I understand cleaning up your
>>>> resources, but usually I am using a VS generated interop library, or
>>>> primary interop assemblies. Making API Calls is somewhat new to me.
>>>>
>>>> Could someone please explain to me why the code at
>>>> http://www.wischik.com/lu/Progr... would leak memory and how to
>>>> fix it. It is not my site, but we used it as an example.
>

Jeremy Hannon

2/27/2007 9:55:00 PM

0

I wanted to thank you once again. We appear to have fixed the problem.
One of the problems was this code that you helped us with. The other
we fixed by recompining Ghostscript 8.54 with another patch that was
checked in (they were not releasing resources).

Again, thanks for you help.

Michael Phillips, Jr. wrote:
> You also need to update SplashImage.
>
> You state there is a memory leak. What is your information source?
>
> Are you getting a GDI resource leak or a memory leak?
>
> I do not see anything else on the unmanaged side.
>
> "Jeremy Hannon" <Jeremy.Hannon.NO_SPAM@AMR.NET> wrote in message
> news:uFe2PgfWHHA.4624@TK2MSFTNGP03.phx.gbl...
>> So, based on your message, I tried this in the CoyTo1bb sample:
>>
>> IntPtr sdc = GetDC(IntPtr.Zero); // First we obtain the DC for the
>> screen
>> // Next, create a DC for the original hbitmap
>> IntPtr hdc = CreateCompatibleDC(sdc);
>> IntPtr tmpObj0 = SelectObject(hdc, hbm);
>> // and create a DC for the monochrome hbitmap
>> IntPtr hdc0 = CreateCompatibleDC(sdc);
>> IntPtr tmpObj1 = SelectObject(hdc0, hbm0);
>> // Now we can do the BitBlt:
>> BitBlt(hdc0, 0, 0, w, h, hdc, 0, 0, SRCCOPY);
>> SelectObject(hdc, tmpObj0);
>> SelectObject(hdc0, tmpObj1);
>>
>> It still appears to be leaking memory, however. What did I miss?
>>
>>
>> Michael Phillips, Jr. wrote:
>>> In the methods CopyTo1bb and SplashImage, the code uses SelectObject to
>>> select the bitmap into a device context before using BitBlt.
>>>
>>> However, the code does not select this bitmap out of the device context
>>> after the BitBlt call. Resources are not freed properly.
>>>
>>> You should use code such as the following:
>>> IntPtr hOldBmp = SelectObject(hdc,hbm);
>>> BitBlt(...);
>>> SelectObject(hdc, hOldBmp);
>>> ...
>>> Delete and or release DC's and other GDI objects
>>>
>>>
>>> "Jeremy Hannon" <Jeremy.Hannon.NO_SPAM@AMR.NET> wrote in message
>>> news:OVI5qoeWHHA.600@TK2MSFTNGP05.phx.gbl...
>>>> I am not the greatest at COM Interop. I understand cleaning up your
>>>> resources, but usually I am using a VS generated interop library, or
>>>> primary interop assemblies. Making API Calls is somewhat new to me.
>>>>
>>>> Could someone please explain to me why the code at
>>>> http://www.wischik.com/lu/Progr... would leak memory and how to
>>>> fix it. It is not my site, but we used it as an example.
>