src/main/haiku/SDL_BeApp.cc
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 29 Oct 2014 20:20:47 +0100
changeset 9201 21d9f9babb30
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed bug 2647 - Memory leak in SDL_AddHintCallback function - SDL_hints.c

Nitz

Variable entry going out of scope leaks the storage it points to, at:

/* Need to add a hint entry for this watcher */
hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
if (!hint) {
return;
}

Patch is attached.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@0
    22
icculus@7981
    23
#if defined(__HAIKU__)
slouken@6044
    24
slouken@0
    25
/* Handle the BeApp specific portions of the application */
slouken@0
    26
slouken@0
    27
#include <AppKit.h>
slouken@0
    28
#include <storage/Path.h>
slouken@0
    29
#include <storage/Entry.h>
slouken@0
    30
#include <unistd.h>
slouken@0
    31
nathanheisey@5916
    32
#include "SDL_BApp.h"	/* SDL_BApp class definition */
slouken@0
    33
#include "SDL_BeApp.h"
slouken@0
    34
#include "SDL_thread.h"
slouken@0
    35
#include "SDL_timer.h"
slouken@0
    36
#include "SDL_error.h"
slouken@0
    37
icculus@7981
    38
#include "../../video/haiku/SDL_BWin.h"
nathanheisey@5916
    39
nathanheisey@5922
    40
#ifdef __cplusplus
nathanheisey@5922
    41
extern "C" {
nathanheisey@5922
    42
#endif
slouken@0
    43
/* Flag to tell whether or not the Be application is active or not */
slouken@0
    44
int SDL_BeAppActive = 0;
slouken@0
    45
static SDL_Thread *SDL_AppThread = NULL;
slouken@0
    46
slouken@1895
    47
static int
slouken@1895
    48
StartBeApp(void *unused)
slouken@0
    49
{
slouken@1895
    50
    BApplication *App;
slouken@0
    51
nathanheisey@5916
    52
    App = new SDL_BApp("application/x-SDL-executable");
slouken@0
    53
slouken@1895
    54
    App->Run();
slouken@1895
    55
    delete App;
slouken@1895
    56
    return (0);
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
/* Initialize the Be Application, if it's not already started */
slouken@1895
    60
int
slouken@1895
    61
SDL_InitBeApp(void)
slouken@0
    62
{
slouken@1895
    63
    /* Create the BApplication that handles appserver interaction */
slouken@1895
    64
    if (SDL_BeAppActive <= 0) {
icculus@5969
    65
        SDL_AppThread = SDL_CreateThread(StartBeApp, "SDLApplication", NULL);
slouken@1895
    66
        if (SDL_AppThread == NULL) {
icculus@7037
    67
            return SDL_SetError("Couldn't create BApplication thread");
slouken@1895
    68
        }
slouken@0
    69
icculus@7987
    70
        /* Change working directory to that of executable */
slouken@1895
    71
        app_info info;
slouken@1895
    72
        if (B_OK == be_app->GetAppInfo(&info)) {
slouken@1895
    73
            entry_ref ref = info.ref;
slouken@1895
    74
            BEntry entry;
slouken@1895
    75
            if (B_OK == entry.SetTo(&ref)) {
slouken@1895
    76
                BPath path;
slouken@1895
    77
                if (B_OK == path.SetTo(&entry)) {
slouken@1895
    78
                    if (B_OK == path.GetParent(&path)) {
slouken@1895
    79
                        chdir(path.Path());
slouken@1895
    80
                    }
slouken@1895
    81
                }
slouken@1895
    82
            }
slouken@1895
    83
        }
slouken@0
    84
slouken@1895
    85
        do {
slouken@1895
    86
            SDL_Delay(10);
slouken@2735
    87
        } while ((be_app == NULL) || be_app->IsLaunching());
slouken@0
    88
slouken@1895
    89
        /* Mark the application active */
slouken@1895
    90
        SDL_BeAppActive = 0;
slouken@1895
    91
    }
slouken@1895
    92
slouken@1895
    93
    /* Increment the application reference count */
slouken@1895
    94
    ++SDL_BeAppActive;
slouken@1895
    95
slouken@1895
    96
    /* The app is running, and we're ready to go */
slouken@1895
    97
    return (0);
slouken@0
    98
}
slouken@0
    99
slouken@0
   100
/* Quit the Be Application, if there's nothing left to do */
slouken@1895
   101
void
slouken@1895
   102
SDL_QuitBeApp(void)
slouken@0
   103
{
slouken@1895
   104
    /* Decrement the application reference count */
slouken@1895
   105
    --SDL_BeAppActive;
slouken@0
   106
slouken@1895
   107
    /* If the reference count reached zero, clean up the app */
slouken@1895
   108
    if (SDL_BeAppActive == 0) {
slouken@1895
   109
        if (SDL_AppThread != NULL) {
slouken@1895
   110
            if (be_app != NULL) {       /* Not tested */
slouken@1895
   111
                be_app->PostMessage(B_QUIT_REQUESTED);
slouken@1895
   112
            }
slouken@1895
   113
            SDL_WaitThread(SDL_AppThread, NULL);
slouken@1895
   114
            SDL_AppThread = NULL;
slouken@1895
   115
        }
slouken@1895
   116
        /* be_app should now be NULL since be_app has quit */
slouken@1895
   117
    }
slouken@0
   118
}
slouken@1895
   119
nathanheisey@5922
   120
#ifdef __cplusplus
nathanheisey@5922
   121
}
nathanheisey@5922
   122
#endif
nathanheisey@5922
   123
nathanheisey@5916
   124
/* SDL_BApp functions */
nathanheisey@5916
   125
void SDL_BApp::ClearID(SDL_BWin *bwin) {
nathanheisey@5919
   126
	_SetSDLWindow(NULL, bwin->GetID());
nathanheisey@5919
   127
	int32 i = _GetNumWindowSlots() - 1;
nathanheisey@5929
   128
	while(i >= 0 && GetSDLWindow(i) == NULL) {
nathanheisey@5919
   129
		_PopBackWindow();
nathanheisey@5916
   130
		--i;
nathanheisey@5916
   131
	}
nathanheisey@5916
   132
}
slouken@6044
   133
icculus@7981
   134
#endif /* __HAIKU__ */
slouken@6044
   135
slouken@6044
   136
/* vi: set ts=4 sw=4 expandtab: */