Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: Weird Memory Issue with Show() in a Form passing struct


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Jul 31, 2017 8:14 PM Last Post By: Michael Thomason Threads: [ Previous | Next ]
Michael Thomason

Posts: 11
Registered: 9/6/10
Weird Memory Issue with Show() in a Form passing struct  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 31, 2017 5:13 PM
All:

Has anyone ran into a similar issue I have found and I have completely stumped.

I have two forms: Form13 and Form14.

Form13 has a Panel and inside the constructor for Form13 a "struct" is created, value set, and memory address passed to Form14. Then Form14 shows it will display the value from the data in Form13.

The struct code:
struct MichaelSTRUCT
{
int jjValue;
};

And the show form command:

TForm14 * Frm14=new TForm14(NULL);
MichaelSTRUCT MichaelData;
MichaelData.jjValue=1963;
MichaelSTRUCT * pMichaelData=&MichaelData;
Frm14->Michael=&MichaelData;
Frm14->Parent=Panel1;
Frm14->Show();

When the example runs, the "jjValue" is not correct and I have noticed it is different in the debugger. The "memory address" target is the same. however the jjValue is different.

This ONLY occurs when the "Parent" panel is used and Form13 has not been shown yet.

So to conclude:

If you have a "form" that is not yet visible, you create a secondary form and host it in a panel, pass some data via a pointer, and call the Show Method the data will be incorrect but memory address target will be correct in the debugger.

Work arounds:

1. In "Form13" create the struct via NEW.
2. Do not use the "panel"
3. Host the panel after the host form is shown.

I can work around this but I am worried this might be a memory design issue.

I will attach the project as well. It is easily reproduced.

I am using C++ Builder Berlin Update 2.

You can download the project here:
https://thinlaunch.sharefile.com/d-s1845bbb9cd549929

Edited by: Michael Thomason on Jul 31, 2017 5:14 PM
Antonio Estevez

Posts: 564
Registered: 4/12/00
Re: Weird Memory Issue with Show() in a Form passing struct  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 31, 2017 7:11 PM   in response to: Michael Thomason in response to: Michael Thomason
El 01/08/2017 a las 2:15, Michael Thomason escribió:
All:

Has anyone ran into a similar issue I have found and I have completely stumped.

I have two forms: Form13 and Form14.

Form13 has a Panel and inside the constructor for Form13 a "struct" is created, value set, and memory address passed to Form14. Then Form14 shows it will display the value from the data in Form13.

The struct code:
struct MichaelSTRUCT
{
int jjValue;
};

And the show form command:

TForm14 * Frm14=new TForm14(NULL);
MichaelSTRUCT MichaelData;
MichaelData.jjValue=1963;
MichaelSTRUCT * pMichaelData=&MichaelData;
Frm14->Michael=&MichaelData;
Frm14->Parent=Panel1;
Frm14->Show();

When the example runs, the "jjValue" is not correct and I have noticed it is different in the debugger. The "memory address" target is the same. however the jjValue is different.

You are passing a pointer to a local variable which is allocated in the stack. Then you are calling the Show() method
which is asynchronous and it returns immediatelyy and the execution continues and exits from the form's constructor. The
variable is not valid at that point and other functions can used its memory to store other locals variables.That is why
the value is different.

This ONLY occurs when the "Parent" panel is used and Form13 has not been shown yet.

So to conclude:

If you have a "form" that is not yet visible, you create a secondary form and host it in a panel, pass some data via a pointer, and call the Show Method the data will be incorrect but memory address target will be correct in the debugger.

Your conclusion is wrong. You are passing a pointer to a memory block that is overwritten by other functions later.

Work arounds:

1. In "Form13" create the struct via NEW.
2. Do not use the "panel"
3. Host the panel after the host form is shown.

Why do not you declare the variable in the form's class instead of a pointer?
class TForm14 : public TForm
{
...
public:		// User declarations
...
	MichaelSTRUCT Michael;
};


__fastcall TForm13::TForm13(TComponent* Owner)
	: TForm(Owner)
{
	TForm14 * Frm14=new TForm14(NULL);
 
	Frm14->Michael.jjValue=1963;
 
	Frm14->Parent=Panel1;
	Frm14->Show();
}


I can work around this but I am worried this might be a memory design issue.

It's an issue in your code.
Michael Thomason

Posts: 11
Registered: 9/6/10
Re: Weird Memory Issue with Show() in a Form passing struct  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 31, 2017 8:14 PM   in response to: Michael Thomason in response to: Michael Thomason
Antonio,

Thank you! Makes perfect sense! Any problem once solved is simple and in looking at the code I can't believe I missed it. Been a long day and MUCH appreciate your response!
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02