Memory Allocation in Managed C++ Results in Exception

T

Tyler

I have what I imagine is a very simple Managed C++ memory allocation
question. I have the following line of code in a static method of a managed
C++ class that always generate a System.NullReferenceException ("Object
reference not set to an instance of an object."):

char * ptvszHexString = new char[100];

Can someone tell me what I am missing?

Thanks, Tyler
 
G

Gary Chang

Hi Tyler,

I test the following managed class in my MC++ program, it works fine:
__gc class testClass{
public:
static void testM1(){
char* ptvszHexString = new char[100];
}
};

Would you please paste some corresponding code snippet(self-contain) and
give some information about your sample program to let us isolate your
problem?


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
T

Tyler

Thanks Gary,

Knowing that it worked for you meant that I was definitely doing something
wrong. I think the problem is that I had not followed the directions (I
didn't know there were any) for setting the properties of a Managed C++ DLL
project. When I attempted to create a small test project, my 'new' would
always generate an unresolved external.

I found the following MSDN article, http://support.microsoft.com/?id=814472,
and followed its directions. When I used the /NOENTRY, msvcrt.lib, removed
nocheckclr.obj, and added the DLLMainCRTStartup@12 reference, my problems
went away. I think my exception was caused because I was attempting to use
'libc' instead of 'msvcrt'.

Can you confirm that I needed to follow the directions in this article in
order to properly setup my project correctly?

Thanks, Tyler


Gary Chang said:
Hi Tyler,

I test the following managed class in my MC++ program, it works fine:
__gc class testClass{
public:
static void testM1(){
char* ptvszHexString = new char[100];
}
};

Would you please paste some corresponding code snippet(self-contain) and
give some information about your sample program to let us isolate your
problem?


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Tyler" <[email protected]>
Subject: Memory Allocation in Managed C++ Results in Exception
Date: Mon, 31 May 2004 09:51:21 -0400
Lines: 12
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vc
NNTP-Posting-Host: g252.lweb.net 209.167.237.252
Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11
phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.vc:37379
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

I have what I imagine is a very simple Managed C++ memory allocation
question. I have the following line of code in a static method of a managed
C++ class that always generate a System.NullReferenceException ("Object
reference not set to an instance of an object."):

char * ptvszHexString = new char[100];

Can someone tell me what I am missing?

Thanks, Tyler
 
G

Gary Chang

Hi Tyler,

I tested your sample code with a MC++ console project, it is really
different with your scenario.

Since the "new" operation of "char* ptvszHexString = new char[100];" is a C
run-time function(in vc7\crt\src\newaop.cpp), the program should load the C
run-time library first, you got the right directions from that KB.


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
T

Tyler

Thank you for that confirmation Gary. My application works fine now that I
have followed the directions in the article.

Thanks, Tyler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top