cannot use 'new' on the reference type; use 'gcnew' instead ? in VS 2005 - ado.net

I am using a C++ language I am getting a strange error when I am try to create a simple object of DataTable its giving error
System::Data::DataTable* myDataTable = new DataTable();
even I tried this
System::Data::DataTable myDataTable = new DataTable();
getting the following error please help.
error C2750: 'System::Data::DataTable' : cannot use 'new' on the reference type; use 'gcnew' instead
error C2440: 'initializing' : cannot convert from 'System::Data::DataTable *' to 'System::Data::DataTable ^

The language you are using is called C++/CLI, not plain C++. In C++/CLI, you can access .NET stuff like DataTable. The semantics are a bit different from raw pointers:
DataTable^ myDataTable = gcnew DataTable;
"^" denotes a managed handle. Under the hood, it's a pointer to an object on the GC heap. You can't do pointer arithmetic on managed handles. You don't delete them manually. The GC will take care of them. It's also free to move the objects around unless they are pinned explicitly. gcnew is used to allocate objects on the managed heap. It returns a handle, not a raw pointer. You can't create .NET reference types on unmanaged C++ heap using new.

Related

ovm printer casting error

I wanted to print out a structure used in a ovm_sequence_item. Since the structure is long, I plan to override table printer knobs by using tbl_printer.knobs.value_width = 100;
Here is the code snipper
virtual function void do_print(ovm_printer printer);
ovm_table_printer tbl_printer;
super.do_print(printer); //print all other fields
$cast(tbl_printer, printer);
tbl_printer.knobs.value_width = 100;
tbl_printer.print_generic("ppid","CppPpid_t",$bits(CppPpid_t),
$psprintf("A=%0b,B=%0b,C=%0d,D=%0d,E=%0d,F=%0x",
struct.A,
struct.B,
struct.C,
struct.D,
struct.E,
struct.F)
);
endfunction: do_print
I am getting this casting error.
Error-[DCF] Dynamic cast failed
*.sv, 58
Casting of source class type 'SIP_SHARED_LIB.ovm_pkg.ovm_tree_printer' to
destination class type 'SIP_SHARED_LIB.ovm_pkg.ovm_table_printer' failed due
to type mismatch.
Please ensure matching types for dynamic cast
Can someone help me what I am doing wrong? How is it getting ovm_tree_printer when I am trying to use ovm_printer?
ovm_printer is just the base class that declares the API for printers. What gets passed around are concrete classes, like ovm_table_printer or ovm_tree_printer. Both of these can be stored in a variable of type ovm_printer.
You're probably passing a tree printer in the print(...) call on your object. If you don't specify any printer, the default printer is used. Per default, this is the table printer, but it could be that this was changed. Look for ovm_default_printer in the package scope.
If someone explicitly wants you to do a tree print, then you can't magically change that to a table print. Best you can do is check if you're doing a table print and if so then change the knobs:
if (!$cast(tbl_printer, printer))
return;
tbl_printer.knobs.value_width = 100;

Arraylist auto complete in eclipse

Im using eclipse and recently my auto complete for arraylist changed for some reason.
after typing: ArrayList <String> myArrayList = new, it usually auto completes the line to: ArrayList<String> myArrayList = new ArrayList<>();, but now when I do it, it auto completes to: ArrayList<String> myArrayList = new ArrayList();(Without the <>).
Does anyone know how to fix it? is it something in eclipse I need to change? or something in the jdk/something like that?
Thanks.
Edit - for some reason it doesn't display the full code, I did define the type of the arraylist to String, so that is not the problem.
Image
Image
ArrayList<> (<> is called Diamond), is valid when the compiled can "infer" this type.
Consider this
ArrayList<String> a = new ArrayList<>();
Here this "diamond" is known to be String.
But when you do a raw ArrayList, the compiler will NOT TYPE CHECK, this can cause serious bugs and crashes in big applications. Generics are meant to reduce bugs.
See
The diamond
Why use Generics?
Try initializing your ArrayList in your constructor (or as a class variable):
ArrayList myArrayList = new ArrayList()
When you need to add to this array list just use myArrayList.add()

Profiling DB2 connection with MiniProfier

I'm trying to add MiniProfiler to my DB2 connections. Below is my simplified code.
public void InitializeConnection()
{
DB2Connection cnn = new DB2Connection("connection String");
var profiler =
new StackExchange.Profiling.Data.ProfiledDbConnection(cnn, MiniProfiler.Current);
IDbCommand c = new DB2Command();
c.Connection = profiler ;
}
My problem is occurring in the last line where the profiler is assigned to the DB2Command's Connection property. I'm getting the below error.
Unable to cast object of type 'StackExchange.Profiling.Data.ProfiledDbConnection' to type 'IBM.Data.DB2.DB2Connection'
I've tried a couple of different casting ideas and nothing has worked out.
I think you're going at it backwards. You're assigning the connection to the ProfiledDbConnection class (as seems to be correct, based on the docs on the MiniProfiler website).
However, you're then creating a DB2-specific command object, and trying to assign the ProfiledDbConnection class to the connection object.
I think what you want to do is call profiler.CreateDbCommand(), which will create a ProfiledDbCommand object that uses the DB2Command class "under the covers".
The DB2Command.Connection property is of the type DB2Connection (as the error message helpfully tells you). Try DbConnection instead:
c.DbConnection = profiler
More in the manual

Lua light userdata

I have a problem with Lua and I don't know if I going in the right direction. In C++ I have a dictionary that I use to pass parameter to a resource manager. This dictionary is really similar to a map of hash and string.
In Lua I want to access to these resource so I need a representation of hashes. Also hashes must be unique cause are used as index in a table. Our hash function is 64bit and I'm working on 32bit enviroment (PS3).
C++ I have somethings like that:
paramMap.insert(std::make_pair(hash64("vehicleId"), std::string("004")));
resourceManager.createResource(ResourceType("Car"), paramMap);
In Lua want use these resources to create a factory for other userdata.
I do stuff like:
function findBike(carId)
bikeParam = { vehicleId = carId }
return ResourceManager.findResource('car', bikeParam)
end
So, sometime parameter are created by Lua, sometime parameter are created by C++.
Cause my hashkey ('vehicleId') is an index of a table it need to be unique.
I have used lightuserdata to implement uint64_t, but cause I'm in a 32bit enviroment I can't simply store int64 in pointer. :(
I have to create a table to store all int64 used by the program and save a reference in userdata.
void pushUInt64(lua_State *L, GEM::GUInt64 v)
{
Int64Ref::Handle handle = Int64Ref::getInstance().allocateSlot(v);
lua_pushlightuserdata(L, reinterpret_cast<void*>(handle));
luaL_setmetatable(L, s_UInt64LuaName);
}
but userdata are never garbage collected. Then my int64 are never released and my table will grow forever.
Also lightuserdata don't keep reference to metadata so they interfere with other light userdata. Checking the implementation the metadata table is added in L->G_->mt_[2].
doing that
a = createLightUserDataType1()
b = createLightUserDataType2()
a:someFunction()
will use the metatable of b.
I thought that metatable where bounded to type.
I'm pretty confused, with the current implementation lightuserdata have a really limited use case.
With Python you have a hash metafunction that is called anytime the type is used as index for a dictionary. It's possible to do something similar?
Sorry for my english, I'm from Italy. :-/

content inserted into ghashtable being destroyed

I have a ghashtable object as member of my class. I have created new object of it at constructor. I am calling this function iteratively. When i checked the size of hashtable at each method call it's giving as 0, even if i eep on adding new key-value pairs.
void myFunction(string inString)
{
string val = "some value";
printf("Size:%d",g_hash_table_size(mTable));
g_hash_table_insert(mTable,(void*)inString.c_str(),(void*)val.c_str());
printf("Size:%d",g_hash_table_size(mTable));
}
What could be the reason behind this problem.
The C++ strings are going out of scope and getting destroyed, leaving the hash table with dangling pointers to invalid memory. I don't know if know if that's the only problem in your program but it's a problem visible from looking at the part you posted.