GHOST/X11 - free memory allocated by XF86VidModeGetAllModeLines(),

Comment from Zr says this crashes but looks like this was because there was no check if the function failed to allocate to begin with.
This commit is contained in:
2013-02-23 23:15:38 +00:00
parent 92f9db6628
commit c42b51ed96
2 changed files with 76 additions and 100 deletions

View File

@@ -81,9 +81,9 @@ getNumDisplaySettings(
return GHOST_kFailure; return GHOST_kFailure;
} }
/* The X11 man page says vidmodes needs to be freed, but doing so causes a if (XF86VidModeGetAllModeLines(dpy, DefaultScreen(dpy), &numSettings, &vidmodes)) {
* segfault. - z0r */ XFree(vidmodes);
XF86VidModeGetAllModeLines(dpy, DefaultScreen(dpy), &numSettings, &vidmodes); }
#else #else
/* We only have one X11 setting at the moment. */ /* We only have one X11 setting at the moment. */
@@ -112,50 +112,46 @@ getDisplaySetting(
GHOST_TInt32 index, GHOST_TInt32 index,
GHOST_DisplaySetting& setting) const GHOST_DisplaySetting& setting) const
{ {
#ifdef WITH_X11_XF86VMODE
int majorVersion, minorVersion;
XF86VidModeModeInfo **vidmodes;
Display *dpy = m_system->getXDisplay(); Display *dpy = m_system->getXDisplay();
int numSettings;
GHOST_ASSERT(display < 1, "Only single display systems are currently supported.\n");
if (dpy == NULL) if (dpy == NULL)
return GHOST_kFailure; return GHOST_kFailure;
#ifdef WITH_X11_XF86VMODE
int majorVersion, minorVersion;
GHOST_ASSERT(display < 1, "Only single display systems are currently supported.\n");
majorVersion = minorVersion = 0; majorVersion = minorVersion = 0;
if (!XF86VidModeQueryVersion(dpy, &majorVersion, &minorVersion)) { if (XF86VidModeQueryVersion(dpy, &majorVersion, &minorVersion)) {
fprintf(stderr, "Error: XF86VidMode extension missing!\n"); XF86VidModeModeInfo **vidmodes;
return GHOST_kFailure; int numSettings;
if (XF86VidModeGetAllModeLines(dpy, DefaultScreen(dpy), &numSettings, &vidmodes)) {
GHOST_ASSERT(index < numSettings, "Requested setting outside of valid range.\n");
setting.xPixels = vidmodes[index]->hdisplay;
setting.yPixels = vidmodes[index]->vdisplay;
setting.bpp = DefaultDepth(dpy, DefaultScreen(dpy));
setting.frequency = calculate_rate(vidmodes[index]);
XFree(vidmodes);
return GHOST_kSuccess;
}
} }
else {
fprintf(stderr, "Warning: XF86VidMode extension missing!\n");
/* fallback to non xf86vmode below */
}
#endif /* WITH_X11_XF86VMODE */
/* The X11 man page says vidmodes needs to be freed, but doing so causes a
* segfault. - z0r */
XF86VidModeGetAllModeLines(dpy, DefaultScreen(dpy), &numSettings, &vidmodes);
GHOST_ASSERT(index < numSettings, "Requested setting outside of valid range.\n");
setting.xPixels = vidmodes[index]->hdisplay;
setting.yPixels = vidmodes[index]->vdisplay;
setting.bpp = DefaultDepth(dpy, DefaultScreen(dpy));
setting.frequency = (float)calculate_rate(vidmodes[index]);
#else
GHOST_ASSERT(display < 1, "Only single display systems are currently supported.\n"); GHOST_ASSERT(display < 1, "Only single display systems are currently supported.\n");
GHOST_ASSERT(index < 1, "Requested setting outside of valid range.\n"); GHOST_ASSERT(index < 1, "Requested setting outside of valid range.\n");
Display *x_display = m_system->getXDisplay();
if (x_display == NULL) { setting.xPixels = DisplayWidth(dpy, DefaultScreen(dpy));
return GHOST_kFailure; setting.yPixels = DisplayHeight(dpy, DefaultScreen(dpy));
} setting.bpp = DefaultDepth(dpy, DefaultScreen(dpy));
setting.xPixels = DisplayWidth(x_display, DefaultScreen(x_display));
setting.yPixels = DisplayHeight(x_display, DefaultScreen(x_display));
setting.bpp = DefaultDepth(x_display, DefaultScreen(x_display));
setting.frequency = 60.0f; setting.frequency = 60.0f;
#endif
return GHOST_kSuccess; return GHOST_kSuccess;
} }
@@ -180,9 +176,8 @@ setCurrentDisplaySetting(
const GHOST_DisplaySetting& setting) const GHOST_DisplaySetting& setting)
{ {
#ifdef WITH_X11_XF86VMODE #ifdef WITH_X11_XF86VMODE
/* Mode switching code ported from Quake 2: /* Mode switching code ported from SDL:
* ftp: ftp.idsoftware.com/idstuff/source/q2source-3.21.zip * See: src/video/x11/SDL_x11modes.c:set_best_resolution
* See linux/gl_glx.c:GLimp_SetMode
*/ */
int majorVersion, minorVersion; int majorVersion, minorVersion;
XF86VidModeModeInfo **vidmodes; XF86VidModeModeInfo **vidmodes;

View File

@@ -612,7 +612,7 @@ int main(int argc, char** argv)
case 'i': case 'i':
i++; i++;
if ( (i + 1) <= validArguments ) if ( (i + 1) <= validArguments )
parentWindow = atoi(argv[i++]); parentWindow = atoi(argv[i++]);
else { else {
error = true; error = true;
printf("error: too few options for parent window argument.\n"); printf("error: too few options for parent window argument.\n");
@@ -645,10 +645,10 @@ int main(int argc, char** argv)
stereomode = (RAS_IRasterizer::StereoMode) atoi(argv[i]); stereomode = (RAS_IRasterizer::StereoMode) atoi(argv[i]);
if (stereomode < RAS_IRasterizer::RAS_STEREO_NOSTEREO || stereomode >= RAS_IRasterizer::RAS_STEREO_MAXSTEREO) if (stereomode < RAS_IRasterizer::RAS_STEREO_NOSTEREO || stereomode >= RAS_IRasterizer::RAS_STEREO_MAXSTEREO)
stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO; stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
if (!strcmp(argv[i], "nostereo")) // ok, redundant but clear if (!strcmp(argv[i], "nostereo")) // ok, redundant but clear
stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO; stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
// only the hardware pageflip method needs a stereo window // only the hardware pageflip method needs a stereo window
else if (!strcmp(argv[i], "hwpageflip")) { else if (!strcmp(argv[i], "hwpageflip")) {
stereomode = RAS_IRasterizer::RAS_STEREO_QUADBUFFERED; stereomode = RAS_IRasterizer::RAS_STEREO_QUADBUFFERED;
@@ -656,22 +656,22 @@ int main(int argc, char** argv)
} }
else if (!strcmp(argv[i], "syncdoubling")) else if (!strcmp(argv[i], "syncdoubling"))
stereomode = RAS_IRasterizer::RAS_STEREO_ABOVEBELOW; stereomode = RAS_IRasterizer::RAS_STEREO_ABOVEBELOW;
else if (!strcmp(argv[i], "anaglyph")) else if (!strcmp(argv[i], "anaglyph"))
stereomode = RAS_IRasterizer::RAS_STEREO_ANAGLYPH; stereomode = RAS_IRasterizer::RAS_STEREO_ANAGLYPH;
else if (!strcmp(argv[i], "sidebyside")) else if (!strcmp(argv[i], "sidebyside"))
stereomode = RAS_IRasterizer::RAS_STEREO_SIDEBYSIDE; stereomode = RAS_IRasterizer::RAS_STEREO_SIDEBYSIDE;
else if (!strcmp(argv[i], "vinterlace")) else if (!strcmp(argv[i], "vinterlace"))
stereomode = RAS_IRasterizer::RAS_STEREO_VINTERLACE; stereomode = RAS_IRasterizer::RAS_STEREO_VINTERLACE;
#if 0 #if 0
// future stuff // // future stuff
else if (!strcmp(argv[i], "stencil") // else if (!strcmp(argv[i], "stencil")
stereomode = RAS_STEREO_STENCIL; // stereomode = RAS_STEREO_STENCIL;
#endif #endif
i++; i++;
stereoParFound = true; stereoParFound = true;
stereoFlag = STEREO_ENABLED; stereoFlag = STEREO_ENABLED;
@@ -733,7 +733,7 @@ int main(int argc, char** argv)
i++; i++;
} }
} }
if ((windowWidth < kMinWindowWidth) || (windowHeight < kMinWindowHeight)) if ((windowWidth < kMinWindowWidth) || (windowHeight < kMinWindowHeight))
{ {
error = true; error = true;
@@ -756,8 +756,7 @@ int main(int argc, char** argv)
//fullScreen = false; // Can't use full screen //fullScreen = false; // Can't use full screen
#endif #endif
if (SYS_GetCommandLineInt(syshandle, "nomipmap", 0)) if (SYS_GetCommandLineInt(syshandle, "nomipmap", 0)) {
{
GPU_set_mipmap(0); GPU_set_mipmap(0);
} }
@@ -765,15 +764,14 @@ int main(int argc, char** argv)
GPU_set_gpu_mipmapping(U.use_gpu_mipmap); GPU_set_gpu_mipmapping(U.use_gpu_mipmap);
// Create the system // Create the system
if (GHOST_ISystem::createSystem() == GHOST_kSuccess) if (GHOST_ISystem::createSystem() == GHOST_kSuccess) {
{
GHOST_ISystem* system = GHOST_ISystem::getSystem(); GHOST_ISystem* system = GHOST_ISystem::getSystem();
assertd(system); assertd(system);
if (!fullScreenWidth || !fullScreenHeight) if (!fullScreenWidth || !fullScreenHeight)
system->getMainDisplayDimensions(fullScreenWidth, fullScreenHeight); system->getMainDisplayDimensions(fullScreenWidth, fullScreenHeight);
// process first batch of events. If the user // process first batch of events. If the user
// drops a file on top off the blenderplayer icon, we // drops a file on top off the blenderplayer icon, we
// receive an event with the filename // receive an event with the filename
system->processEvents(0); system->processEvents(0);
@@ -798,8 +796,7 @@ int main(int argc, char** argv)
// those may change during the game and persist after using Game Actuator // those may change during the game and persist after using Game Actuator
GlobalSettings gs; GlobalSettings gs;
do do {
{
// Read the Blender file // Read the Blender file
BlendFileData *bfd; BlendFileData *bfd;
@@ -814,19 +811,17 @@ int main(int argc, char** argv)
bfd = load_game_data(basedpath); bfd = load_game_data(basedpath);
if (!bfd) if (!bfd) {
{
// just add "//" in front of it // just add "//" in front of it
char temppath[242]; char temppath[242];
strcpy(temppath, "//"); strcpy(temppath, "//");
strcat(temppath, basedpath); strcat(temppath, basedpath);
BLI_path_abs(temppath, pathname); BLI_path_abs(temppath, pathname);
bfd = load_game_data(temppath); bfd = load_game_data(temppath);
} }
} }
else else {
{
bfd = load_game_data(BLI_program_path(), filename[0]? filename: NULL); bfd = load_game_data(BLI_program_path(), filename[0]? filename: NULL);
} }
@@ -836,13 +831,11 @@ int main(int argc, char** argv)
usage(argv[0], isBlenderPlayer); usage(argv[0], isBlenderPlayer);
error = true; error = true;
exitcode = KX_EXIT_REQUEST_QUIT_GAME; exitcode = KX_EXIT_REQUEST_QUIT_GAME;
} }
else else {
{
#ifdef WIN32 #ifdef WIN32
#if !defined(DEBUG) #if !defined(DEBUG)
if (closeConsole) if (closeConsole) {
{
system->toggleConsole(0); // Close a console window system->toggleConsole(0); // Close a console window
} }
#endif // !defined(DEBUG) #endif // !defined(DEBUG)
@@ -864,8 +857,7 @@ int main(int argc, char** argv)
titlename = maggie->name; titlename = maggie->name;
// Check whether the game should be displayed full-screen // Check whether the game should be displayed full-screen
if ((!fullScreenParFound) && (!windowParFound)) if ((!fullScreenParFound) && (!windowParFound)) {
{
// Only use file settings when command line did not override // Only use file settings when command line did not override
if ((scene->gm.playerflag & GAME_PLAYER_FULLSCREEN)) { if ((scene->gm.playerflag & GAME_PLAYER_FULLSCREEN)) {
//printf("fullscreen option found in Blender file\n"); //printf("fullscreen option found in Blender file\n");
@@ -885,16 +877,16 @@ int main(int argc, char** argv)
// Check whether the game should be displayed in stereo // Check whether the game should be displayed in stereo
if (!stereoParFound) if (!stereoParFound) {
{
if (scene->gm.stereoflag == STEREO_ENABLED) { if (scene->gm.stereoflag == STEREO_ENABLED) {
stereomode = (RAS_IRasterizer::StereoMode) scene->gm.stereomode; stereomode = (RAS_IRasterizer::StereoMode) scene->gm.stereomode;
if (stereomode == RAS_IRasterizer::RAS_STEREO_QUADBUFFERED) if (stereomode == RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
stereoWindow = true; stereoWindow = true;
} }
} }
else else {
scene->gm.stereoflag = STEREO_ENABLED; scene->gm.stereoflag = STEREO_ENABLED;
}
if (!samplesParFound) if (!samplesParFound)
aasamples = scene->gm.aasamples; aasamples = scene->gm.aasamples;
@@ -908,8 +900,7 @@ int main(int argc, char** argv)
scene->gm.dome.tilt = domeTilt; scene->gm.dome.tilt = domeTilt;
if (domeMode > 0) if (domeMode > 0)
scene->gm.dome.mode = domeMode; scene->gm.dome.mode = domeMode;
if (domeWarp) if (domeWarp) {
{
//XXX to do: convert relative to absolute path //XXX to do: convert relative to absolute path
domeText= BKE_text_load(G.main, domeWarp, ""); domeText= BKE_text_load(G.main, domeWarp, "");
if (!domeText) if (!domeText)
@@ -928,23 +919,21 @@ int main(int argc, char** argv)
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
setGamePythonPath(G.main->name); setGamePythonPath(G.main->name);
#endif #endif
if (firstTimeRunning) if (firstTimeRunning) {
{
firstTimeRunning = false; firstTimeRunning = false;
if (fullScreen) if (fullScreen) {
{
#ifdef WIN32 #ifdef WIN32
if (scr_saver_mode == SCREEN_SAVER_MODE_SAVER) if (scr_saver_mode == SCREEN_SAVER_MODE_SAVER)
{ {
app.startScreenSaverFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency, app.startScreenSaverFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
stereoWindow, stereomode, aasamples); stereoWindow, stereomode, aasamples);
} }
else else
#endif #endif
{ {
app.startFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency, app.startFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
stereoWindow, stereomode, aasamples, (scene->gm.playerflag & GAME_PLAYER_DESKTOP_RESOLUTION)); stereoWindow, stereomode, aasamples, (scene->gm.playerflag & GAME_PLAYER_DESKTOP_RESOLUTION));
} }
} }
else else
@@ -968,8 +957,7 @@ int main(int argc, char** argv)
vector<STR_String> parts = path.Explode('\\'); vector<STR_String> parts = path.Explode('\\');
#endif // WIN32 #endif // WIN32
STR_String title; STR_String title;
if (parts.size()) if (parts.size()) {
{
title = parts[parts.size()-1]; title = parts[parts.size()-1];
parts = title.Explode('.'); parts = title.Explode('.');
if (parts.size() > 1) if (parts.size() > 1)
@@ -977,8 +965,7 @@ int main(int argc, char** argv)
title = parts[0]; title = parts[0];
} }
} }
else else {
{
title = "blenderplayer"; title = "blenderplayer";
} }
#ifdef WIN32 #ifdef WIN32
@@ -989,16 +976,15 @@ int main(int argc, char** argv)
else else
#endif #endif
{ {
if (parentWindow != 0) if (parentWindow != 0)
app.startEmbeddedWindow(title, parentWindow, stereoWindow, stereomode, aasamples); app.startEmbeddedWindow(title, parentWindow, stereoWindow, stereomode, aasamples);
else else
app.startWindow(title, windowLeft, windowTop, windowWidth, windowHeight, app.startWindow(title, windowLeft, windowTop, windowWidth, windowHeight,
stereoWindow, stereomode, aasamples); stereoWindow, stereomode, aasamples);
} }
} }
} }
else else {
{
app.StartGameEngine(stereomode); app.StartGameEngine(stereomode);
exitcode = KX_EXIT_REQUEST_NO_REQUEST; exitcode = KX_EXIT_REQUEST_NO_REQUEST;
} }
@@ -1014,11 +1000,9 @@ int main(int argc, char** argv)
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
python_main = KX_GetPythonMain(scene); python_main = KX_GetPythonMain(scene);
#endif // WITH_PYTHON #endif // WITH_PYTHON
if (python_main) if (python_main) {
{
char *python_code = KX_GetPythonCode(maggie, python_main); char *python_code = KX_GetPythonCode(maggie, python_main);
if (python_code) if (python_code) {
{
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
gpg_nextframestate.system = system; gpg_nextframestate.system = system;
gpg_nextframestate.app = &app; gpg_nextframestate.app = &app;
@@ -1036,16 +1020,14 @@ int main(int argc, char** argv)
fprintf(stderr, "ERROR: cannot yield control to Python: no Python text data block named '%s'\n", python_main); fprintf(stderr, "ERROR: cannot yield control to Python: no Python text data block named '%s'\n", python_main);
} }
} }
else else {
{ while (run) {
while (run)
{
run = GPG_NextFrame(system, &app, exitcode, exitstring, &gs); run = GPG_NextFrame(system, &app, exitcode, exitstring, &gs);
} }
} }
app.StopGameEngine(); app.StopGameEngine();
/* 'app' is freed automatic when out of scope. /* 'app' is freed automatic when out of scope.
* removal is needed else the system will free an already freed value */ * removal is needed else the system will free an already freed value */
system->removeEventConsumer(&app); system->removeEventConsumer(&app);
@@ -1060,7 +1042,8 @@ int main(int argc, char** argv)
// Dispose the system // Dispose the system
GHOST_ISystem::disposeSystem(); GHOST_ISystem::disposeSystem();
} else { }
else {
error = true; error = true;
printf("error: couldn't create a system.\n"); printf("error: couldn't create a system.\n");
} }
@@ -1090,5 +1073,3 @@ int main(int argc, char** argv)
return error ? -1 : 0; return error ? -1 : 0;
} }