|
extern char and Allegro dialogs |
Elverion
Member #6,239
September 2005
|
I can't for the life of me figure out exactly what is going on here. Apparently, having an extern char being used in an Allegro dialog is causing a crash. In "main.h" and "mydlg.h", lets say, I have declared an extern char *. In "main.cpp", I have something similar to this: char *szString = new char[MAXLEN];. I am sure to strcpy(zsString, "something"), and check to make sure it works as a string. Everything looks ok, even when I use printf in "mydlg.cpp". The problem comes when trying to use that string in the (dp) field of a dialog with do_dialog. The program just crashes. If I replace the string with (void*)"test" in the dialog, it does not crash. Some code would probably help, eh? I'll be excluding any code that seems irrelevant. main.h //useless junk here #include "connect_gui.h" extern char *connect_ip; main.cpp #include "main.h" char *connect_ip = new char[IP_MAXLEN+1]; int main() { // setup stuff here InitConGUI(); do_dialog(con_dlg, -1); // <-- crash happens HERE // extra, unrelated stuff here } connect_gui.h #include "main.h" extern char *connect_ip; connect_gui.cpp
Now, for that line I made note of in connect_gui.cpp...If I uncomment that line there, and comment out the same line in main.cpp [declaring connect_ip in connect_gui.cpp rather than main.cpp], the program works just fine. Because of this , I believe the problem is due to the externs. In InitConGUI, use of the string works just fine no mater where the string is declared. Because of this, I'm quite sure the problem is not with the string itself, but something with the externs and the dialogs in combination. As far as fixing this goes, I'm out of ideas. I could declare a temp string in connect_gui.cpp, yes, but I would rather fix the problem at the root rather than hack a cheep fix in there. Any ideas? -- |
Kitty Cat
Member #2,815
October 2002
|
I'm going to assume you are intentionally not showing more code, and that this isn't all of it. The order objects are created/initialized between compilation units isn't defined. 'connect_ip' is probably NULL by the time the DIALOG struct is created, and is later set to something else. Never assume the order of global object initialization. It's typically a bad idea to allocate in global space like that. It might be best of you do the new in InitConGUI: if(!connect_ip) { connect_ip = new char[IP_MAXLEN+1]; con_dlg[1].dp = (void*)connect_ip; } Don't forget to delete[] connect_ip after you're all done with that dialog and before the program is shutdown. -- |
Elverion
Member #6,239
September 2005
|
Hmm, you're right. I never thought about the order of global initialization. I've fixed my code based on your input. And, yes, I might not have been clear in my first post(or maybe I said too much and you just skimmed through it), but I did intentionally leave out a lot of code that I thought would have just been cluttering up the post, and was unrelated to the problem. For the record, I originally did have the "connect_ip = new char[IP_MAXLEN+1];" part inside a function, but due to moving it around so much trying to fix the crashing leads to a mess till I can get the damn thing working. My current, working "connection_gui.cpp" is as follows:
You're suggestion for the InitConGUI really helped clean things up a bit and makes it easier to work with. Thanks. Now that I've got it working, I just need to figure out how I want to remedy the fact that my dialog is blue and black, rather than white and gray. Looks completely different in the program than it does in the DLG editor found in the Utils section of this site. I suppose using 8bit color would work. I would give you a cookie, but it appears that I created the topic wrong and cannot. It's the thought that counts, right? -- |
Dennis
Member #1,090
July 2003
|
Quote:
set_dialog_color(con_dlg, 255, 23); Now that I've got it working, I just need to figure out how I want to remedy the fact that my dialog is blue and black, rather than white and gray.
Must be because of the values you pass there for foreground and background color. --- 0xDB | @dennisbusch_de --- |
James Stanley
Member #7,275
May 2006
|
I don't have a clue if this helps, but I usually have a d_clear_proc as the first object in my dialogs, I don't know if you'd left it out as it's irrelevant. I don't know, really. I know this doesn't make any difference at all, but why do use (void*) as the type for the strings? I've used (char*) in all of mine. Does the Allegro Documentation say my way is bad or does it make no difference? |
|