• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FSX Memory Management

Messages
533
Country
france
Hello,
I have a very strange question, not sure if anyone can help, but at least I try...
I have a piece of C++ code that uses GDI+, where all the pens and brushes are deleted (using delete) to be re-created (using new) with different parameters. This used to work perfectly, until I had a strange bug that caused a CTD. I searched and found this part of the code is the cause, so I debugged by writing the address of the pen and brush objects.

In FS2004, it is very straightforward: when a brush object is located at address X in memory, I delete it, create it again, and the address of the new created object still X, unchanged.

In FSX, the object has address X in memory, deleted, created, and appears with address Y. How can you explain this? Is there any difference in the way memory is managed in FSX? Hard to believe...

In both cases, the code is exactly the same, same computer, same OS, same compiler, and different behavior. If by any chance someone experienced a similar situation, please let me know.

Thanks for any help...
Eric

PS. This memory allocation difference is not a problem in itself. It appears that in some specific conditions (standard 737 loaded first with batteries off, then my aircraft loaded and batteries switched on), it crashes and the display of memory addresses shows that an address is corrupted, as if it was erased by a memory overflow or something I can't explain...
 
Q: are you using more than 4 GB of RAM and FSXA or FSXSP2? If so, then maybe its Large Address Aware flag has some relationship (?) (FS9 is compiled without this flag)

Tom
 
No, I have only 2 GB on my PC.
I will investigate further, I can't understand how the exact same code can behave differently in fs9 and FSX.

I'll post here if I find something new.

Thanks for your help
Eric
 
Is your code *relying* on the pointed address being the same ? If yes, well...it shouldn't and if it really needs to be that way, you should ensure that you register the changes before trying to access that memory again.

When you do a delete, the address pointed by the pointer variable is marked free to be used for other objects that will be created so, you can't rely on getting the same address again with a subsequent new operation, especially if you allocated other memory after deleting that pointer.

But the question is, why are you reusing pointers to gdi+ objects ? How many different gdi+ brushes, pens, fonts your program needs ?

If they are not hundreds of them, it's usually best if you allocate each one to its own pointer variable at the start of the program ( in a gauge, inside the module_init() call), then delete them all at the end (module_deinit(), for example). This way you'll likely encounter less issues with possible memory leaks, since your code will be easier to read (all allocations in single place, all deallocations in another single place), and you'll get best performances too, because you won't risk micro-pauses because you asked the virtual memory manager for some memory when your program was in the middle of something.

If you have many gauges, it's way better to create a "palette" of ready made brushes, pens and fonts, with all the colors and styles you might possibly need, allocate all the memory for them at the start, and reclaim it at the end.

If you need to have many variations of a brushes/pens, for example to create a gdi+ gauge that can be dimmed or changed in contrast or color saturation, etc, consider creating an array of brushes (always at the start of the program), which can be accessed very fast, because everything is already pre-created, you won't risk slowdowns by the new/delete operations and the actual initialization of the gdi object made by gdi+ itself when you create the object from scratch.

And, in order to work in a cleaner way with pointers, forget new and delete altogether, and use the std::auto_ptr data type. This is the new "C++ way" of dealing with pointers, and it's very comfortable to use, for example:

Code:
std::auto_ptr<SolidBrush> myGdiBrush;

This declares the myGdiBrush variable as a smart pointer to a GDI+ SolidBrush object


Code:
myGdiBrush.reset( new SolidBrush( Color( A, R, G, B ) ) );

This initialize (or re-initialize) the pointer to the gdi+ object with some alpha, r, g, and b values.

To USE the pointer with GDI+, you call get() on it, this way:

Code:
g.FillRectangle( myGdiBrush.get(), x, y, w, h );

The nice thing is, there's are no worries about deleting! When the object destructor is called, the memory allocated with new will be deleted automatically, without you having to worry about it. Where this happens, depends where you declared the pointer so, for example, if you declared an auto_ptr at the main() level, its destructor will be called when the program exits, and if you declared inside a function, it will be called when the function will go auto of scope, without the need to write any code for it, because it's all generated by the compiler.

Isn't C++ fun ?
 
Is your code *relying* on the pointed address being the same ? If yes, well...it shouldn't and if it really needs to be that way, you should ensure that you register the changes before trying to access that memory again.

When you do a delete, the address pointed by the pointer variable is marked free to be used for other objects that will be created so, you can't rely on getting the same address again with a subsequent new operation, especially if you allocated other memory after deleting that pointer.

I fully understand this. I know that when I delete a pointer, the memory is freed, and when I create a new object, I allocate a free memory segment, which is not necessarily the one I have just freed. But it seems logical to me that when I have just created a "hole" in the memory, this hole is proposed as a new memory segment when needed. This is the best way, for the system, to limit the number of holes in the memory. Anyway, I don't mind, the most important for me is to get a pointer at a memory address that is allocated for me. This is the case here, so everything should work fine, but 1) I was surprised to see the memory allocation behavior was different in fs9 and FSX (whereas it is supposed to be managed by the OS) and 2) it appears that in some specific conditions, it crashes FSX. My investigations showed the problem does not come from the memory allocation, it is because one of my pointer value is corrupted (I'm still searching why) and consequently I delete something that is not an allocated pointer.

Now to answer your question, the solution was implemented this way because there is no other way... I have developed a very dynamic system, based on a configuration files, so that any EFIS display can be implemented with the same code through a configuration file. In addition, the dynamcity is very important during the development phase. It means I can have my gauges loaded in an aircraft, change the configuration file, and press the button to see the change immediately. I agree with you, there can be a delay at this time because I delete and re-create all the pens and brushes, but I can wait 5 ms when I refresh my gauge after a configuration change, it is not even visible and it does not happen at each refresh cycle. In addition, I don't really manage pens and brushes, I manage "resources" that can be pens, dashed pens, solid brushes or hatch brushes. This is why I have to delete and recreate. Because of the possible changes in the configuration, the resource number X can be a pen and become a brush after a configuration refresh.

In fact, I have something like this:
- In the header file, my pens and brushes are declared as tables of pointers:
Code:
Pen** _pen;
Brush** _brush;
- In the constructor, the pen and brush tables are created and the pointer values are initialized to NULL:
Code:
_pen = new Pen*[_nbRsrc];
_brush = new Brush*[_nbRsrc];
[...]
for (int i=0; i < _nbRsrc; i++)
{
	_pen[i] = 0;
	_brush[i] = 0;
}
- In my code, when the gauge is refresh after a configuration change:
Code:
for (int i=0; i < _nbRsrc; i++)
{
	if ((_resource[i].type == PEN) || (_resource[i].type == PEN_BRUSH) || (_resource[i].type == HATCHED_BRUSH))
	{
		Debug("Creating resource #%d (PEN %X) with color %X", i, _pen[i], _resource[i].color.GetValue());
		if (_pen[i])
			delete _pen[i];
		_pen[i] = new Pen(_resource[i].color, _resource[i].thickness);
		Debug("Pen created @ %X", _pen[i]);
	}
	if ((_resource[i].type == BRUSH) || (_resource[i].type == PEN_BRUSH))
	{
		Debug("Creating resource #%d (BRUSH %X) with color %X", i, _brush[i], _resource[i].color.GetValue());
		if (_brush[i])
			delete _brush[i];
		_brush[i] = new SolidBrush(_resource[i].color);
		Debug("Brush created @ %X", _brush[i]);
	}
}
type, color and thickness are read in the configuration file.

My initial question comes from the Debug messages I see. In fs9, I see a freed memory segment is immediately reallocated when I make a "new", which looks logical to me, but in FSX, it reallocates a completly different address.

Eric
 
You can forget my previous post, I found the reason why it was different in fs9 and FSX. It appeared the compilation flags were not exactly the same and something was wrong in the FSX version. Now I have the same behavior in both cases (fs9 and FSX). The debugger shows this (example for brush #2):

- first iteration, nothing is created:

[5228] CRJ NG>Creating resource #2 (BRUSH 0) with color FF000000
[5228] CRJ NG>Brush created @ 16649A80

As the brush was NULL, it is created at adress 16649A80.

- at next iteration, after a refresh:

[5228] CRJ NG>Creating resource #2 (BRUSH 16649A80) with color FF0C000C
[5228] CRJ NG>Brush created @ 16649A80

As you can see, adress 16649A80 is freed and immediately reused when the brush is re-created using a different color. The new brush is created at the same address.
Note that if it was created at a different address, it wouldn't be a problem because the pointer is stored in my table. But I'm happy to see the memory allocation works in a logical way, and works the same in fs9 and FSX.

Now the real problem is for resource #67, which is a brush. At first iteration, I have:

[5228] CRJ NG>Creating resource #67 (BRUSH 0) with color FF000000
[5228] CRJ NG>Brush created @ 46EEEA0

This is correct. It was NULL and my code created a brush at address 46EEEA0.

- At the next iteration, I have this:

[5228] CRJ NG>Creating resource #67 (BRUSH 16652FE8) with color FF000C00

The address was 46EEEA0 and my code now "believes" it is 16652FE8, so it deletes a pointer at this address and... crash.
It appears something is wrong in my code because the pointer stored in my table changes from 46EEEA0 to 16652FE8 without reason. I will look into this, wish me good luck :)

Eric
 
I found it !!
There was something wrong in my code, I found the problem, fixed it and everything now works fine.

Conclusion: the memory allocator is not crazy, FSX is not crazy, but my code was wrong...

Eric
 
In addition, I don't really manage pens and brushes, I manage "resources" that can be pens, dashed pens, solid brushes or hatch brushes. This is why I have to delete and recreate. Because of the possible changes in the configuration, the resource number X can be a pen and become a brush after a configuration refresh.

I understand your approach and, with this kind of design, it seemed like a good candidate for polymorphism, create a base "resource" class, then derive from it pen, brushes, etc, and implement their differences using virtual functions.

But in any case, it was good you eventually found the problem. Have a look at the std::auto_ptr, it helps a lot with this kind of pointer headaches, it's *almost* like having a garbage collection system (like in C#), without the performance impact of it.
 
But in any case, it was good you eventually found the problem. Have a look at the std::auto_ptr, it helps a lot with this kind of pointer headaches, it's *almost* like having a garbage collection system (like in C#), without the performance impact of it.

I've enjoyed following this thread. First, I'm happy that Eric identified the problem and found the solution. Secondly, thank you Umberto for providing some new (to me at least) "food for thought."

I've archived this "tip" for possible future use, should I actually ever find the time to make use of it! :rotfl:
 
I understand your approach and, with this kind of design, it seemed like a good candidate for polymorphism, create a base "resource" class, then derive from it pen, brushes, etc, and implement their differences using virtual functions.

But in any case, it was good you eventually found the problem. Have a look at the std::auto_ptr, it helps a lot with this kind of pointer headaches, it's *almost* like having a garbage collection system (like in C#), without the performance impact of it.
You're right, i didn't think about polymorphism for this usage, even though I used it for something else (I have a vector gauge mother class that can be a PFD, a ND, an EICAS or a CDU). I didn't know about std::auto_ptr, I will look at that closely, even if I am a little "old school" and I like to manage my pointers myself. Thanks for the tip anyway.

Now if you want to know what was wrong in my code, I can explain, it may be useful for someone:
In a derived class of PFD (derivated for a specific aircraft), I had a specific case where a brush had to change its color according to the context, sometimes green, sometimes white. As I had 2 other brushes that were green and white, I changed the brush color by setting my brush as equal to the green brush or to the white brush. I didn't realize that I was setting pointers here !! So when the delete part arrived, I was deleting twice the same pointer... The solution was easy: instead of equalizing the pointers, I used SetColor on my brush to change it's color without changing the pointers.

Eric

PS. I realize that each time I have a problem, explaining it on a forum always helped me finding the solution, certainly because formalizing the problem for the others is 50% of the solution. This is the interest of sharing :)
 
Last edited:
Back
Top