String not updating once TextCtrl cleared Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
FelixDev
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Aug 20, 2021 7:02 pm

String not updating once TextCtrl cleared

Post by FelixDev »

Hello fellow programmers,

I have come across one, hopefully the last, error in my programme that I am unable to fix on my own.

My current problem is the following:

I have written a routine, which is able to create a (pseudo) random combination of 32 characters. It displays the content in a wxTextCtrl called “tc”.
So far so good, but when I have cleared “tc” once, the programme exits with an error. I will attach a screenshot of it and the code at the end of my question.

Any help is appreciated!

Felix
The code:

Code: Select all

void MyFrame::Random() {
	
	int val = 0;

	char a1[] = { 'A', 'B' ,'C' ,'D' ,'E' ,'F' ,'G' ,'H' ,'I' ,'J' ,
		'K' ,'L' ,'M' ,'N' ,'O' ,'P' ,'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,
		'W' ,'X' ,'Y' ,'Z' ,'a' ,'b' ,'c' ,'d' ,'e' ,'f' ,'g' ,'h' ,
		'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,'p' ,'q' ,'r' ,'s' ,'t' ,
		'u' ,'v' ,'w' ,'x' ,'y' ,'z' ,'0' ,'1' ,'2' ,'3' ,'4' ,'5' ,
		'6' ,'7' ,'8' ,'9' ,')' ,'=' ,'{' ,'*' ,'>' ,'+', '?',',','@',
		'|','-','[','.','/', ']','}','!','"', ':','^', '$','#','%',
		';','_', '&','(','<', '`','~'};
	
	srand(time(NULL));

	for (int i = 0; i < 32; i++) { 

		val = (rand() % 76);

		str2.push_back(a1[val]); 
		
		
	} 

	const std::string s1 = str2;

	wxString NewSeed = wxString(s1);
	
	tc->SetLabel(NewSeed);
	
}
Attachments
Screenshot (36).png
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: String not updating once TextCtrl cleared

Post by jpo234 »

FelixDev wrote: Sat Oct 30, 2021 12:21 pm Hello fellow programmers,

I have come across one, hopefully the last, error in my programme that I am unable to fix on my own.

My current problem is the following:

I have written a routine, which is able to create a (pseudo) random combination of 32 characters. It displays the content in a wxTextCtrl called “tc”.
So far so good, but when I have cleared “tc” once, the programme exits with an error. I will attach a screenshot of it and the code at the end of my question.

Any help is appreciated!

Felix
The code:

Code: Select all

void MyFrame::Random() {
	
	int val = 0;

	char a1[] = { 'A', 'B' ,'C' ,'D' ,'E' ,'F' ,'G' ,'H' ,'I' ,'J' ,
		'K' ,'L' ,'M' ,'N' ,'O' ,'P' ,'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,
		'W' ,'X' ,'Y' ,'Z' ,'a' ,'b' ,'c' ,'d' ,'e' ,'f' ,'g' ,'h' ,
		'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,'p' ,'q' ,'r' ,'s' ,'t' ,
		'u' ,'v' ,'w' ,'x' ,'y' ,'z' ,'0' ,'1' ,'2' ,'3' ,'4' ,'5' ,
		'6' ,'7' ,'8' ,'9' ,')' ,'=' ,'{' ,'*' ,'>' ,'+', '?',',','@',
		'|','-','[','.','/', ']','}','!','"', ':','^', '$','#','%',
		';','_', '&','(','<', '`','~'};
	
	srand(time(NULL));

	for (int i = 0; i < 32; i++) { 

		val = (rand() % 76);

		str2.push_back(a1[val]); 
		
		
	} 

	const std::string s1 = str2;

	wxString NewSeed = wxString(s1);
	
	tc->SetLabel(NewSeed);
	
}
There are a number of things I'd do differently.
  • Don't create the character array every time on the stack, make it a) static and b) a true c++ std::array. Gives you free range checking.
  • Use the C++ RNG from <random>. Much better...
  • str2 looks suspicious. And why all the shuffling around from str2 to s1 to NewSeed? Doesn't make any sense at all.
If I had to guess from this snippet, I'd say that str2 isn't valid any longer.
FelixDev
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Aug 20, 2021 7:02 pm

Re: String not updating once TextCtrl cleared

Post by FelixDev »

Hello jpo234,

thank you for your reply! I have removed the shuffling around - it was there, because IntelliSense was so solw, that it had me thinking, I had to do it that way.

Do you have any suggestions on how to deal with an invalid string?

Thanks in advance!

Best,
Felix!
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: String not updating once TextCtrl cleared

Post by jpo234 »

FelixDev wrote: Sat Oct 30, 2021 4:57 pm Do you have any suggestions on how to deal with an invalid string?
If this is still your str2, I can't say anything. But as a guess, it's somehow destroyed. Without seeing it's life cycle, its impossible to say why and when it's gone.
FelixDev
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Aug 20, 2021 7:02 pm

Re: String not updating once TextCtrl cleared

Post by FelixDev »

Hello,

the only time I do anything to it is in the loop of Random(). It is created with std::string str2, modified in the loop you already saw and that's it. I then clear the wxTextCtrl that has its contents via tc->SetLabel(str2) with tc->Clear(), but I assume it doesn't do anything to the string itself, right?

Best,
Felix
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: String not updating once TextCtrl cleared

Post by doublemax »

I can't spot any error in the code you posted.

What does the code look like where you clear the text control?
Why is str2 not declared locally in that method? Where else is it used?

And it would be helpful to get a proper call stack.
Use the source, Luke!
FelixDev
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Aug 20, 2021 7:02 pm

Re: String not updating once TextCtrl cleared

Post by FelixDev »

Hello doublemax,

I have declared str2 locally now, since it isn't used anywhere else.

The code, where I clear the text control is called once a button is pressed and looks like this:

Code: Select all

void MyFrame::OnStop(wxCommandEvent& e) {
	
	timer.Stop();
	tc->Clear();
	tc->SetEditable(true);
	Phase = 0; 
	Run = 0;
	wxString RunNo = wxString::Format(wxT("%i"), Run);
	st6->SetLabel(RunNo);
	panel1->Refresh();
	
	Val1 = -1;
	Val2 = 0;
	Val3 = 0;
	
}
What do you mean by a proper call stack? Should I put a break point there and show you the events before?

Best,
Felix
FelixDev
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Aug 20, 2021 7:02 pm

Re: String not updating once TextCtrl cleared

Post by FelixDev »

Hello guys,

this is so embarrassing. #-o

I actually just spotted my error, once I put a breakpoint at the entry of the Random() routine. I realized, it wasn't even called the second time, because I put a function that accessed the value inside the text control before the call of the function declaring this value. I was so focused on the string itself, that I forgot the fact, that I am reading the text control's value with another function, which fails, when tc has no value.

I appreciate your help and am sorry for wasting your time! ](*,)

Best,
Felix
Post Reply