pointers set correctly but evaluating later as NULL

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

pointers set correctly but evaluating later as NULL

Postby Azazaz » Mon Apr 27, 2015 1:27 pm

I have a class with static pointers that allows me to reference the same tables in some components as in the main project, i.e., I set the pointer in the project when launched and they are then be available to the components.

This approach has worked well through several iterations of Rad Studio, but it is not working in XE7. When I set them, they seem to be getting the values set correctly, but when they are referenced in the components, they are evaluating as NULL.

I can even set the pointer manually by setting a break point right before it is accessed in the component, and use the Eval/modify debug facility, but just as soon as the code tries to access it, the pointer still evaluates as NULL.

Anyone have any ideas about what could be causing this?
Azazaz
BCBJ Guru
BCBJ Guru
 
Posts: 105
Joined: Wed Sep 26, 2012 9:08 am

Re: pointers set correctly but evaluating later as NULL

Postby rlebeau » Tue Apr 28, 2015 1:03 am

Azazaz wrote:When I set them, they seem to be getting the values set correctly, but when they are referenced in the components, they are evaluating as NULL.

I can even set the pointer manually by setting a break point right before it is accessed in the component, and use the Eval/modify debug facility, but just as soon as the code tries to access it, the pointer still evaluates as NULL.


Please show actual examples.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1530
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: pointers set correctly but evaluating later as NULL

Postby Azazaz » Tue Apr 28, 2015 7:47 am

Okay.

I have a class called TACoordinator that contains static pointers and static methods. It is part of the components and is never instantiated, i.e., it contains nothing that isn't static.

ACoordinator.h is an include in both the main project and in the components.

When compiling and running the main app, in the FormShow method for the main form, which gets called first (I've traced it with the debugger and it does get called first), there are lines that read like this:

TACoordinator::s_DiaTbl = TblModule->tDia;

This is setting the static pointer to the table; tDia is a design-time table component in the TblModule and has the exact same type as the s_DiaTbl pointer in TACoordinator. TblModule.h is an include in the main form's unit.

If I set a break point immediately after the assignment, evaluating the code at that point shows that the s_DiaTbl pointer has been set correctly and to the right value.

A bit later in the code, a method in one of the components is called that references the
TACoordinator::s_DiaTbl pointer, i.e.,

TACoordinator::s_DiaTbl->Open();

At that point, it evaluates as being set to NULL. But that code definitely executes after the assignment mentioned previously. I traced it with the debugger just to be sure.

As I said, even if I change it manually at this point before the code attempts to access the pointer, it remains NULL.

Also, there is no other place in the code anywhere that s_DiaTbl gets set (except, of course, in the ACoordinator.cpp file which initializes all the static pointers). Just to be sure, I checked with grep and there is no other assignment anywhere, as I expected.

Now the thing is, this code has not changed since it compiled and ran perfectly in XE5 (and I think XE6), so I have no real reason to think there is something wrong with the code.

Does XE7 handle static pointers differently than previous versions?

It will depend on a couple of others how much of the code I can actually share, but if this is not enough information, I will try to provide more, though I don't know how it would shed any more light anyway as all I could do is show you code that simply confirms what I've already said.
Azazaz
BCBJ Guru
BCBJ Guru
 
Posts: 105
Joined: Wed Sep 26, 2012 9:08 am

Re: pointers set correctly but evaluating later as NULL

Postby Azazaz » Tue Apr 28, 2015 7:54 am

I just remembered something that I've done differently this time out.

I am compiling the components and the main project with "Run the C++ Compiler in a Separate Process" checked.

I have never done that previously, but I keep getting saveMEM and exit code 1 errors, and this is the only "fix" I have been able to find that does any good.

But I'm wondering now if that could have anything to with things?

Could compiling them this way be causing the components and main project to always run in different processes? It doesn't really seem reasonable to me, but I thought I'd mention it.
Azazaz
BCBJ Guru
BCBJ Guru
 
Posts: 105
Joined: Wed Sep 26, 2012 9:08 am

Re: pointers set correctly but evaluating later as NULL

Postby Azazaz » Tue Apr 28, 2015 1:26 pm

Ignore the last post. I managed to get both components and the main project to compile without the separate process option checked. It didn't make any difference.

Also, I've noticed that some of the static pointers get set and some don't, even though the code is the same and even though the lines of code setting the values are called in the same place, i.e., I've got --

TACoordinator::s_MVwTbl = TblManageMod->tMVw;
TACoordinator::s_DiaTbl = TblManageMod->tDia;

-- right next to each other in the same FormShow method. They are exactly the same type, defined the same way, etc., etc. One gets set and stays set. The other seems to get set, but then turns to NULL somehow. And I am seeing the same behavior with some of my other static pointers, some set elsewhere and some not.
Azazaz
BCBJ Guru
BCBJ Guru
 
Posts: 105
Joined: Wed Sep 26, 2012 9:08 am

Re: pointers set correctly but evaluating later as NULL

Postby rlebeau » Tue Apr 28, 2015 9:07 pm

Azazaz wrote:I have a class called TACoordinator that contains static pointers and static methods. It is part of the components and is never instantiated, i.e., it contains nothing that isn't static.
<snip>


Actual code snippets would have been better than long descriptions.

Azazaz wrote:If I set a break point immediately after the assignment, evaluating the code at that point shows that the s_DiaTbl pointer has been set correctly and to the right value.

A bit later in the code, a method in one of the components is called that references the
TACoordinator::s_DiaTbl pointer, i.e.,

TACoordinator::s_DiaTbl->Open();

At that point, it evaluates as being set to NULL. But that code definitely executes after the assignment mentioned previously. I traced it with the debugger just to be sure.


This implies to me that the MainForm and the component are actually accessing two completely different copies of the TACoordinator class. Such as if your component is implemented in a runtime package, and you are compiling with Runtime Packages enabled. Is that actually the case? Did you try using the debugger to look at the actual memory address of the s_DiaTbl static variable to make sure both codebases are actually looking at the same variable in memory?

Azazaz wrote:As I said, even if I change it manually at this point before the code attempts to access the pointer, it remains NULL.


Assuming there really are two separate copies of the TACoordinator class, you are liking having the debugger assign the MainForm's copy of the class, not the component's copy. But then evaluating the component's copy, not the MainForm's copy.

Azazaz wrote:Does XE7 handle static pointers differently than previous versions?


Not that I am aware of.

Azazaz wrote:It will depend on a couple of others how much of the code I can actually share, but if this is not enough information, I will try to provide more, though I don't know how it would shed any more light anyway as all I could do is show you code that simply confirms what I've already said.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1530
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: pointers set correctly but evaluating later as NULL

Postby rlebeau » Tue Apr 28, 2015 9:09 pm

Azazaz wrote:Also, I've noticed that some of the static pointers get set and some don't, even though the code is the same and even though the lines of code setting the values are called in the same place
...
And I am seeing the same behavior with some of my other static pointers, some set elsewhere and some not.


All the more reason to suspect that you have separate copies of the TACoordinator class being used in different modules.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1530
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: pointers set correctly but evaluating later as NULL

Postby Azazaz » Tue Apr 28, 2015 9:24 pm

Okay.

I re-created the project for XE7 from scratch as I've been told in the past it is not a good idea to try to import projects from previous versions, but I may not have been previously building with runtime packages or vice versa.

I will double check everything you mentioned and see if there is a problem as you described.

Thanks for the feedback.
Azazaz
BCBJ Guru
BCBJ Guru
 
Posts: 105
Joined: Wed Sep 26, 2012 9:08 am

Re: pointers set correctly but evaluating later as NULL

Postby Azazaz » Tue Apr 28, 2015 10:12 pm

Okay. I unchecked 'Link with runtime packages' in the main project and that fixed it.

It's kind of embarrassing to be always so very dumb about everything, but I am semi-disabled with chronic pain, am taking care of someone a whole lot worse, more or less full-time, and have very limited time and energy to study this stuff.

So, you know, that's my lame excuse for being such an idiot.

I keep trying, but I have never understood a whole lot of things. I guess I need to research this whole runtime packages issue (among several others) and try (if I'm up to it) to understand it a little better.

Thanks again for your help!
Azazaz
BCBJ Guru
BCBJ Guru
 
Posts: 105
Joined: Wed Sep 26, 2012 9:08 am


Return to Technical

Who is online

Users browsing this forum: No registered users and 8 guests