Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2005

Tip: Looking for answers? Try searching our database.

__gc new and __nogc new

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian Lazarus - 06 Jan 2005 18:09 GMT
Hello,

I tried to confirm that two different heap allocation calls were being made,
i.e., one for __gc and one for __nogc. However, the addresses of the calls
(as seen in disassembly) are not what I expected. As you can see the four
calls in tmain are all to the same routine, while the one in the class
constructor is different.  Does anybody know what's going on here?

Thanks

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
__value class VClass
{
public:
     VClass()
     : p(new int(0)) // call F9BB56E3
     { }
     int* p;
};

int _tmain()
{
     int* p1 = new int(0); // call dword ptr ds:[00965A00h]
     int __nogc* p2 = __nogc new int(0); // call dword ptr ds:[00965A00h]
     Int32* p3 = new Int32(0); // call dword ptr ds:[00965A00h]
     Int32 __gc* p4 = new Int32(0); // call dword ptr ds:[00965A00h]
     VClass v;

     return 0;
}
Stu Smith - 11 Jan 2005 12:24 GMT
> Hello,
>
[quoted text clipped - 3 lines]
> calls in tmain are all to the same routine, while the one in the class
> constructor is different.  Does anybody know what's going on here?

This is a guess, and I'd be interested to hear a more definitive answer.

First I used Reflector to have a look at what's going on:

public static unsafe int modopt(CallConvCdecl) main()
{
     VClass class1;
     int num4 = <Module>.new(4);
     int* numPtr2 = (num4 == 0) ? null : ((int*) num4);
     int num3 = <Module>.new(4);
     int* numPtr1 = (num3 == 0) ? null : ((int*) num3);
     int num2 = <Module>.new(4);
     ref int numRef2 = (num2 == 0) ? 0 : num2;
     int num1 = <Module>.new(4);
     ref int numRef1 = (num1 == 0) ? 0 : num1;
     class1 = new VClass();
     class1 = new VClass();
     return 0;
}

[PreserveSig, MethodImpl(MethodImplOptions.Unmanaged,
MethodCodeType=MethodCodeType.Native), SuppressUnmanagedCodeSecurity]
public static unsafe void* modopt(CallConvCdecl) @new(uint);

[StructLayout(LayoutKind.Sequential)]
internal struct VClass
{
     public unsafe int* p;
     public VClass();
}

public unsafe VClass()
{
     int num2;
     int num1 = <Module>.new(4);
     if (num1 != 0)
     {
           num1[0] = 0;
           num2 = num1;
     }
     else
     {
           num2 = 0;
     }
     this.p = (int*) num2;
}

I think therefore that because main() and new() have the same calling
convention (cdecl), the call can be made directly. However, when calling
from VClass (which presumably uses something different, stdcall maybe?), a
thunk has to be used to change between the calling conventions.

Stu

> Thanks
>
[quoted text clipped - 20 lines]
>       return 0;
> }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.