Skip to content

Commit

Permalink
SYMBIAN: Avoid deleting argv if no command line arguments have been set
Browse files Browse the repository at this point in the history
Also apply style fixes from #3170.
  • Loading branch information
fedor4ever committed Jul 21, 2021
1 parent 9e67b1e commit 4eb00e9
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions backends/platform/symbian/src/SymbianMain.cpp
Expand Up @@ -21,40 +21,39 @@
*/


#include <bacline.h> // CCommandLineArguments
#include <bacline.h> // CCommandLineArguments

#include "backends/platform/symbian/src/portdefs.h"
#include "base/main.h"
#include "backends/platform/symbian/src/SymbianOS.h"

extern "C"
{
// Include the snprintf and vsnprintf implementations as 'C' code
// Include the snprintf and vsnprintf implementations as 'C' code.
#include "vsnprintf.h"
}

// Symbian SDL_Main implementation
// Redirects standard io, creates Symbian specific SDL backend (inherited from main SDL)
// Symbian SDL_Main implementation.
// Redirects standard io, creates Symbian specific SDL backend (inherited from main SDL).
int main(int argc, char *argv[]) {
//

// Set up redirects for stdout/stderr under Symbian.
// Code copied from SDL_main.
//

// Symbian does not like any output to the console through any *print* function
char STDOUT_FILE[256], STDERR_FILE[256]; // shhh, don't tell anybody :)
// Symbian does not like any output to the console through any *print* function.
char STDOUT_FILE[256], STDERR_FILE[256]; // Shhh, don't tell anybody :)
strcpy(STDOUT_FILE, Symbian::GetExecutablePath());
strcpy(STDERR_FILE, Symbian::GetExecutablePath());
strcat(STDOUT_FILE, "scummvm.stdout.txt");
strcat(STDERR_FILE, "scummvm.stderr.txt");

/* Flush the output in case anything is queued */
// Flush the output in case anything is queued.
fclose(stdout);
fclose(stderr);

/* Redirect standard input and standard output */
// Redirect standard input and standard output.
FILE *newfp = freopen(STDOUT_FILE, "w", stdout);
if (newfp == NULL) { /* This happens on NT */
if (newfp == NULL) { // This happens on NT.
#if !defined(stdout)
stdout = fopen(STDOUT_FILE, "w");
#else
Expand All @@ -65,7 +64,7 @@ int main(int argc, char *argv[]) {
#endif
}
newfp = freopen(STDERR_FILE, "w", stderr);
if (newfp == NULL) { /* This happens on NT */
if (newfp == NULL) { // This happens on NT.
#if !defined(stderr)
stderr = fopen(STDERR_FILE, "w");
#else
Expand All @@ -75,34 +74,34 @@ int main(int argc, char *argv[]) {
}
#endif
}
setbuf(stderr, NULL); /* No buffering */
setbuf(stderr, NULL); // No buffering.

// Create our OSystem instance
// Create our OSystem instance.
g_system = new OSystem_SDL_Symbian();
assert(g_system);

// Pre initialize the backend
// Pre initialize the backend.
g_system->init();

#ifdef DYNAMIC_MODULES
PluginManager::instance().addPluginProvider(new SDLPluginProvider());
#endif

// catch input params and pass to argv/argc
// Catch input params and pass to argv/argc.
CCommandLineArguments *cmdline = CCommandLineArguments::NewL();
if (!cmdline) {
error("Failure to alloc CCommandLineArguments!");
return -1;
}

argc = cmdline->Count();
if (argc > 1) {
debug("console arg count by CCommandLineArguments: %d", argc);
argv = new char* [argc];
HBufC8 *buf = HBufC8::NewMax(20); //this suffice for most cases
HBufC8 *buf = HBufC8::NewMax(20); // This should suffice for most cases.
for (TInt i = 0; i < argc; ++i) {
TPtrC arg = cmdline->Arg(i);
argv[i] = new char[arg.Length() + 1](); //hold zero terminated string
argv[i] = new char[arg.Length() + 1](); // Hold zero terminated string.
if (arg.Length() > buf->Length())
buf->ReAlloc(arg.Length());

Expand All @@ -117,14 +116,16 @@ int main(int argc, char *argv[]) {
// Invoke the actual ScummVM main entry point:
int res = scummvm_main(argc, argv);

// clear argv
for (TInt i = 0; i < argc; ++i) {
delete[] argv[i];
// Clear argv.
if (argc > 1) { //avoid multiple User 42 panics at exit
for (TInt i = 0; i < argc; ++i) {
delete[] argv[i];
}
delete[] argv;
}
delete[] argv;
delete cmdline;

// Free OSystem
// Free OSystem.
g_system->destroy();

return res;
Expand Down

0 comments on commit 4eb00e9

Please sign in to comment.