473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Crash in dynamic cast in dll code

Hi All,
I am trying to invoke methods in c++ code in two ways, one by
compiling the calling code with the called code and running it
standalone, other by making a dll of the called code and then calling
it. First case works fine but in the 2nd case, I encounter a crash
at the following line
CThread* ct = &CThread::GetCurrentThread();
TEventHandler* eh = dynamic_cast< TEventHandler* >( ct );

Any hints why the crash occurs?

Thanks,
Peeyush

Oct 25 '07 #1
6 3442
On Oct 25, 11:23 am, Peeyus...@gmail.com wrote:
Hi All,
I am trying to invoke methods in c++ code in two ways, one by
compiling the calling code with the called code and running it
standalone, other by making a dll of the called code and then calling
it. First case works fine but in the 2nd case, I encounter a crash
at the following line
CThread* ct = &CThread::GetCurrentThread();
TEventHandler* eh = dynamic_cast< TEventHandler* >( ct );

Any hints why the crash occurs?
The most obvious thing is that you're dynamic casting to a pointer. If
that fails it returns NULL. Do you test eh for being NULL as you
always should with bare pointers?
That's the most obvious culprit, to me at least. What's the difference
in the code that's doing this in the local code and in the DLL? Can a
CThread* be safely cast to a TEventHandler*.
Oct 25 '07 #2
Make sure that you compile the DLL with RTTI support. otherwise there
is no
RTTI info to support the dynamic_cast and then it will crash.

On 10 25 , 6 23 , Peeyus...@gmail.com wrote:
Hi All,
I am trying to invoke methods in c++ code in two ways, one by
compiling the calling code with the called code and running it
standalone, other by making a dll of the called code and then calling
it. First case works fine but in the 2nd case, I encounter a crash
at the following line
CThread* ct = &CThread::GetCurrentThread();
TEventHandler* eh = dynamic_cast< TEventHandler* >( ct );

Any hints why the crash occurs?

Thanks,
Peeyush

Oct 25 '07 #3
Thanks for the prompt responses,
Jonathan,
The pointer is not null and assigned a valid address.( I checked in
debug)
I cannot find any differences between the two ways of invoking the
code.

Liao,
I have RTTI check enabled now. Now it does not crash at that line but
at the line next to it.
If I continue ,it repeats this arbitrary behavior at random lines,
even at
simple String assignments.

TString fddevName = "DAL";

Could it be a problem of stack
overflow bacause of insufficient memory to the program when running in
dll mode?

Oct 26 '07 #4
Environment Specs-
Win XP, VC++ 6.0

Oct 26 '07 #5
Pe*******@gmail.com wrote:
even at
simple String assignments.

TString fddevName = "DAL";

Could it be a problem of stack
overflow bacause of insufficient memory to the program when running in
dll mode?
What the * is a TString? "DAL" is not a string, it's an array
of char[].

The other Visual Studio gotcha is to make sure all your DLL's are
compiled with the same C++ runtimes (see the CODE GENERATION TAB)
or to be darned sure that you do all your memory allocation and
deallocation in the same DLL (i.e., no inlined allocations in the
headers!)>

Your best bet is to take this off-topic qustion over to one
of the groups that has "microsoft" in its name.
Oct 26 '07 #6
On Oct 26, 5:41 am, Peeyus...@gmail.com wrote:
Thanks for the prompt responses,
Jonathan,
The pointer is not null and assigned a valid address.( I checked in
debug)
I cannot find any differences between the two ways of invoking the
code.

Liao,
I have RTTI check enabled now. Now it does not crash at that line but
at the line next to it.
If I continue ,it repeats this arbitrary behavior at random lines,
even at
simple String assignments.

TString fddevName = "DAL";

Could it be a problem of stack
overflow bacause of insufficient memory to the program when running in
dll mode?
I would doubt it's run out of memory to be honest. If it's crashing at
a random point later then you've obviously already corrupted the heap.
I did find, a while back, a technique to log all allocations and de-
allocations. Try googling for something like that to see where you're
corrupting the heap.

Otherwise, as Ron Natalie suggests it could be down to using different
runtimes in the different components. Check that in the project
settings. Also make sure that you're linking against the same version
of the libraries that supply these types. I've seen errors similar to
this where different versions of the STL are used by the third party
vendor and STL types are passed on the command line.

Failing that, the types you have are all MS types so try a MS C++
newsgroup.

Oct 26 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: adrian | last post by:
hi all this is my first post to this group, so pls bear with me while i try to make some sense. i am trying to create a sproc which uses dynamic sql to target a particuar table eg. '.' and...
2
by: Richard | last post by:
Hi all, I had a stored procedure that i wanted to be able to pass parameters to so i used dynamic SQL. I am getting very strange errors though when i try to execute it. My code works just fine,...
2
by: Dom | last post by:
Hi, I am trying to get a dynamic table going. You click a button to add a row. In the newly created row a button to delete the row is placed (try the code example below). In Netscape, pressing...
4
by: Jürgen Devlieghere | last post by:
We encounter a crash every now and then at a client, and now I'm starting to doubt the fundamentals of life :-) We have a list of structs. struct SContactProperty { public:...
5
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
6
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
6
by: DaTurk | last post by:
Hi, I have three interfaces lets call them parent, child-A, and child-B. I want to have one variable, that can be either child-A, or child-B at run time. I just can't seem to figure out how to...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
1
by: Pacific Fox | last post by:
Hi all, I have a SQL statement that allows paging and dynamic sorting of the columns, but what I can't figure out without making the SQL a dynamic string and executing it, or duplicating the SQL...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.