• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Strange dynamic_cast problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Strange dynamic_cast problem


  • Subject: Strange dynamic_cast problem
  • From: Eric Gorr <email@hidden>
  • Date: Mon, 3 Nov 2008 17:09:16 -0500

I have a strange problem with a dynamic_cast.

I have put together a simple test project which can be found at:

  http://ericgorr.net/LibraryLoading.zip

which demonstrates the problem.

Unfortunately, this is a bit complicated, but I will do my best to explain this simply.

In Shared.h, there are the definitions of two purely virtual classes - A & B. B is a subclass of A.

In LibraryA, there is a implementation of class B called BImp. There is a function called GetA which returns a pointer to an instance of BImp and returns an A*.

In LibraryB, there is a function called test. This function takes a void *, which will end up being a function pointer to the GetA function from LibraryA.

The problem comes in the test function with the line:

	B* myB = dynamic_cast<B*> (myA);

The dynamic_cast fails and myB is assigned NULL. This _should not_ fail because myA is an instance of class B.

However, I can make this dynamic_cast succeed, if in main.cp, which is a part of the Application target, I set:

#define CASE_A 1

which allows

		A *myA = functionPtr();
		B *myB = dynamic_cast<B*> (myA);

to be executed before the test function from LibraryB is called.


Any idea why this allows it to work? Any idea why it is failing in the first place?



Any thoughts or comments would be appreciated.



p.s.

In main.cp, there are two #define's.

#define CASE_A 0
#define CASE_B 0

If both are set to zero, it will crash. This is simplest crashing case.

If CASE_A is 1, it will work.

If CASE_B is 1, it will crash and this is the case closest to the real case I am working with.

Of course, both CASE_A & CASE_B cannot be 1.


thank you.




_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Strange dynamic_cast problem
      • From: "Chris Suter" <email@hidden>
  • Prev by Date: Re: Failed to launch simulator: Operation failed with underlying error -43.
  • Next by Date: Re: IB references from static library
  • Previous by thread: Re: xCode shortCuts on spanish keyboard...
  • Next by thread: Re: Strange dynamic_cast problem
  • Index(es):
    • Date
    • Thread