SDL_crashRwops

:?

hello,
i’m making a game engine and for compiling my scripts, i’m using SDL_Rwops
the scripting language is squirrel and it works, because it use’s FILE*, but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src, SQInteger sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir

this is the debug log from GDB:
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: F:\projects\cpp\advanced audio game engine
Adding source dir: F:\projects\cpp\advanced audio game engine
Adding file: F:\projects\cpp\advanced audio game engine\bin\Debug\aage.exe
Changing directory to: F:/projects/cpp/ADVANC~1/bin/Debug
Set variable: PATH=.;C:\Dev-Cpp\bin;C:\Dev-Cpp;C:\Python34;C:\Python34\Scripts;C:\Program
Files (x86)\cmake\bin;C:\Program Files (x86)\Intel\iCLS
Client;C:\Program Files\Intel\iCLS
Client;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program
Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files
(x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\Intel®
Management Engine Components\DAL;C:\Program Files\Intel\Intel®
Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel®
Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel®
Management Engine Components\IPT;C:\Program
Files\TortoiseSVN\bin;C:\Program Files (x86)\Skype\Phone

[debug]Command-line: C:\Dev-Cpp\bin\gdb.exe -nx -fullname -quiet
-args F:/projects/cpp/ADVANC~1/bin/Debug/aage.exe
[debug]Working dir : F:\projects\cpp\ADVANC~1\bin\Debug

Starting debugger: C:\Dev-Cpp\bin\gdb.exe -nx -fullname -quiet -args
F:/projects/cpp/ADVANC~1/bin/Debug/aage.exe
done

[debug]> set prompt >>>>>>cb_gdb:

Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints

[debug]Reading symbols from F:/projects/cpp/ADVANC~1/bin/Debug/aage.exe…
[debug]done.
debug
[debug]>>>>>>cb_gdb:
[debug]> show version
[debug]GNU gdb (GDB) 7.9.1
[debug]Copyright © 2015 Free Software Foundation, Inc.
[debug]License GPLv3+: GNU GPL version 3 or later
http://gnu.org/licenses/gpl.html
[debug]This is free software: you are free to change and redistribute it.
[debug]There is NO WARRANTY, to the extent permitted by law. Type
"show copying"
[debug]and “show warranty” for details.
[debug]This GDB was configured as “i686-w64-mingw32”.
[debug]Type “show configuration” for configuration details.
[debug]For bug reporting instructions, please see:
[debug]http://www.gnu.org/software/gdb/bugs/.
[debug]Find the GDB manual and other documentation resources online at:
[debug]http://www.gnu.org/software/gdb/documentation/.
[debug]For help, type “help”.
[debug]Type “apropos word” to search for commands related to “word”.
[debug]>>>>>>cb_gdb:
[debug]> set confirm off

Debugger name and version: GNU gdb (GDB) 7.9.1

[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 0
[debug]>>>>>>cb_gdb:
[debug]> set new-console on
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor att
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> source C:\Program Files
(x86)\CodeBlocks\share\codeblocks/scripts/stl-views-1.0.3.gdb
[debug]>>>>>>cb_gdb:
[debug]> directory F:/projects/cpp/ADVANC~1/
[debug]Source directories searched: F:/projects/cpp/ADVANC~1;$cdir;$cwd
[debug]>>>>>>cb_gdb:
[debug]> set args hello.aage -c -o hello.exe
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: F:\projects\cpp\ADVANC~1\bin\Debug\aage.exe
hello.aage -c -o hello.exe

Child process PID: 93804

[debug][New Thread 93804.0x1750c]
[debug][New Thread 93804.0x16db0]
[debug][New Thread 93804.0x16cec]
[debug][New Thread 93804.0x1705c]
[debug]Program received signal SIGSEGV, Segmentation fault.
[debug]0x00000000 in ?? ()
[debug]>>>>>>cb_gdb:

Program received signal SIGSEGV, Segmentation fault.
In ?? () ()

[debug]> bt 30
[debug]#0 0x00000000 in ?? ()
[debug]#1 0x00430010 in Sqrat::rwops_write (dest=0x3331d0,
src=0x28f2fa, sz=2) at F:/projects/cpp/advanced audio game
engine/include/sqrat/sqratScript.h:44
[debug]#2 0x004601dc in sq_writeclosure (v=0x3ad0c0, w=0x42ffe0
<Sqrat::rwops_write(void*, void*, int)>, up=0x3331d0) at
F:\projects\cpp\advanced audio game engine\src\squirrel\sqapi.cpp:1156
[debug]#3 0x005f0bce in Sqrat::Script::CompileToMemoryabi:cxx11
(this=0x28fe3c) at F:/projects/cpp/advanced audio game
engine/include/sqrat/sqratScript.h:262
[debug]#4 0x004302a3 in comp (scr=…, out=…) at
F:\projects\cpp\advanced audio game engine\src\main.cpp:75
[debug]#5 0x004318d7 in WinMain at 16 (hInst=0x400000, hPrevInst=0x0,
cmdLine=0x19a3d9e “hello.aage -c -o hello.exe”, showtype=10) at
F:\projects\cpp\advanced audio game engine\src\main.cpp:298
[debug]#6 0x0070663d in main ()
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1 0x00430010 in Sqrat::rwops_write (dest=0x3331d0,
src=0x28f2fa, sz=2) at F:/projects/cpp/advanced audio game
engine/include/sqrat/sqratScript.h:44
[debug]F:\projects\cpp\advanced audio game
engine\include\sqrat\sqratScript.h:44:1335:beg:0x430010
[debug]>>>>>>cb_gdb:

#1 0x00430010 in Sqrat::rwops_write (dest=0x3331d0, src=0x28f2fa,
sz=2) at F:/projects/cpp/advanced audio game
engine/include/sqrat/sqratScript.h:44
F:\projects\cpp\advanced audio game
engine\include\sqrat\sqratScript.h:44:1335:beg:0x430010
At F:\projects\cpp\advanced audio game engine\include\sqrat\sqratScript.h:44

[debug]> bt 30
[debug]#0 0x00000000 in ?? ()
[debug]#1 0x00430010 in Sqrat::rwops_write (dest=0x3331d0,
src=0x28f2fa, sz=2) at F:/projects/cpp/advanced audio game
engine/include/sqrat/sqratScript.h:44
[debug]#2 0x004601dc in sq_writeclosure (v=0x3ad0c0, w=0x42ffe0
<Sqrat::rwops_write(void*, void*, int)>, up=0x3331d0) at
F:\projects\cpp\advanced audio game engine\src\squirrel\sqapi.cpp:1156
[debug]#3 0x005f0bce in Sqrat::Script::CompileToMemoryabi:cxx11
(this=0x28fe3c) at F:/projects/cpp/advanced audio game
engine/include/sqrat/sqratScript.h:262
[debug]#4 0x004302a3 in comp (scr=…, out=…) at
F:\projects\cpp\advanced audio game engine\src\main.cpp:75
[debug]#5 0x004318d7 in WinMain at 16 (hInst=0x400000, hPrevInst=0x0,
cmdLine=0x19a3d9e “hello.aage -c -o hello.exe”, showtype=10) at
F:\projects\cpp\advanced audio game engine\src\main.cpp:298
[debug]#6 0x0070663d in main ()
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1 0x00430010 in Sqrat::rwops_write (dest=0x3331d0,
src=0x28f2fa, sz=2) at F:/projects/cpp/advanced audio game
engine/include/sqrat/sqratScript.h:44
[debug]F:\projects\cpp\advanced audio game
engine\include\sqrat\sqratScript.h:44:1335:beg:0x430010
[debug]>>>>>>cb_gdb:

#1 0x00430010 in Sqrat::rwops_write (dest=0x3331d0, src=0x28f2fa,
sz=2) at F:/projects/cpp/advanced audio game
engine/include/sqrat/sqratScript.h:44
F:\projects\cpp\advanced audio game
engine\include\sqrat\sqratScript.h:44:1335:beg:0x430010

i’m using GCC 5.1 MinGW-w64 with code::blocks 13.12

???-??-?? ??:?? GMT+??:??, brightening-eyes <@Amir_Ramezani>:> hello,

i’m making a game engine and for compiling my scripts, i’m using SDL_Rwops
the scripting language is squirrel and it works, because it use’s FILE*, but
i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src, SQInteger sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir

As far as I can tell, you’re not assigning any memory for the RWops to work
on. SDL_AllocRW() does not create a working buffer, only the rwops
object. I would instead use SDL_RWFromMem(buf, buf_size) to create the
rwops.

It also looks like your memory buffer only has a size of 1 byte. And
you’re not freeing this memory nor the rwops. There may be other issues,
but try allocating the buffer like so:
char buf[MAX_BUF_SIZE];

And then before the compile() function is over:
SDL_RWclose®;

Jonny DOn Saturday, November 7, 2015, brightening-eyes <amir.ramezani1370 at gmail.com> wrote:

hello,
i’m making a game engine and for compiling my scripts, i’m using SDL_Rwops
the scripting language is squirrel and it works, because it use’s FILE*,
but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src, SQInteger sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir

i don’t know how mutch size should be allocated into memory
because maybe the script get’s longer and the size may not be sufficient
thanks

???-??-?? ??:?? GMT+??:??, Jonathan Dearborn :> As far as I can tell, you’re not assigning any memory for the RWops to work

on. SDL_AllocRW() does not create a working buffer, only the rwops
object. I would instead use SDL_RWFromMem(buf, buf_size) to create the
rwops.

It also looks like your memory buffer only has a size of 1 byte. And
you’re not freeing this memory nor the rwops. There may be other issues,
but try allocating the buffer like so:
char buf[MAX_BUF_SIZE];

And then before the compile() function is over:
SDL_RWclose®;

Jonny D

On Saturday, November 7, 2015, brightening-eyes <@Amir_Ramezani> wrote:

hello,
i’m making a game engine and for compiling my scripts, i’m using
SDL_Rwops
the scripting language is squirrel and it works, because it use’s FILE*,
but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src, SQInteger
sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir

If you can’t specify a maximum size, then you have to count it
dynamically. Either count up the size you need, calculate it somehow, or
cut it into smaller parts (each with a reasonable max size) that you can
feed into a growable container (e.g. a file or a C++ string object). Just
remember to delete temporarily allocated memory.

Jonny DOn Sat, Nov 7, 2015 at 9:16 PM, Amir Ramezani <amir.ramezani1370 at gmail.com> wrote:

i don’t know how mutch size should be allocated into memory
because maybe the script get’s longer and the size may not be sufficient
thanks

???-??-?? ??:?? GMT+??:??, Jonathan Dearborn <@Jonathan_Dearborn>:

As far as I can tell, you’re not assigning any memory for the RWops to
work
on. SDL_AllocRW() does not create a working buffer, only the rwops
object. I would instead use SDL_RWFromMem(buf, buf_size) to create the
rwops.

It also looks like your memory buffer only has a size of 1 byte. And
you’re not freeing this memory nor the rwops. There may be other issues,
but try allocating the buffer like so:
char buf[MAX_BUF_SIZE];

And then before the compile() function is over:
SDL_RWclose®;

Jonny D

On Saturday, November 7, 2015, brightening-eyes <amir.ramezani1370 at gmail.com> wrote:

hello,
i’m making a game engine and for compiling my scripts, i’m using
SDL_Rwops
the scripting language is squirrel and it works, because it use’s FILE*,
but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src, SQInteger
sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

i’ve specified the maximam size of 256, i think it’s sufficient
but, how to make char* to grow itself up?

???-??-?? ?:?? GMT+??:??, Jonathan Dearborn :> If you can’t specify a maximum size, then you have to count it

dynamically. Either count up the size you need, calculate it somehow, or
cut it into smaller parts (each with a reasonable max size) that you can
feed into a growable container (e.g. a file or a C++ string object). Just
remember to delete temporarily allocated memory.

Jonny D

On Sat, Nov 7, 2015 at 9:16 PM, Amir Ramezani <@Amir_Ramezani> wrote:

i don’t know how mutch size should be allocated into memory
because maybe the script get’s longer and the size may not be sufficient
thanks

???-??-?? ??:?? GMT+??:??, Jonathan Dearborn :

As far as I can tell, you’re not assigning any memory for the RWops to
work
on. SDL_AllocRW() does not create a working buffer, only the rwops
object. I would instead use SDL_RWFromMem(buf, buf_size) to create the
rwops.

It also looks like your memory buffer only has a size of 1 byte. And
you’re not freeing this memory nor the rwops. There may be other
issues,
but try allocating the buffer like so:
char buf[MAX_BUF_SIZE];

And then before the compile() function is over:
SDL_RWclose®;

Jonny D

On Saturday, November 7, 2015, brightening-eyes <@Amir_Ramezani> wrote:

hello,
i’m making a game engine and for compiling my scripts, i’m using
SDL_Rwops
the scripting language is squirrel and it works, because it use’s
FILE*,
but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src, SQInteger
sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

char* doesn’t grow and the SDL_RWops wouldn’t be able to handle it if you
tried. If you need more explicit help with this, you can email me off-list.

Jonny DOn Sat, Nov 7, 2015 at 10:27 PM, Amir Ramezani <amir.ramezani1370 at gmail.com> wrote:

i’ve specified the maximam size of 256, i think it’s sufficient
but, how to make char* to grow itself up?

???-??-?? ?:?? GMT+??:??, Jonathan Dearborn <@Jonathan_Dearborn>:

If you can’t specify a maximum size, then you have to count it
dynamically. Either count up the size you need, calculate it somehow, or
cut it into smaller parts (each with a reasonable max size) that you can
feed into a growable container (e.g. a file or a C++ string object).
Just
remember to delete temporarily allocated memory.

Jonny D

On Sat, Nov 7, 2015 at 9:16 PM, Amir Ramezani < amir.ramezani1370 at gmail.com> wrote:

i don’t know how mutch size should be allocated into memory
because maybe the script get’s longer and the size may not be sufficient
thanks

???-??-?? ??:?? GMT+??:??, Jonathan Dearborn <@Jonathan_Dearborn>:

As far as I can tell, you’re not assigning any memory for the RWops to
work
on. SDL_AllocRW() does not create a working buffer, only the rwops
object. I would instead use SDL_RWFromMem(buf, buf_size) to create
the

rwops.

It also looks like your memory buffer only has a size of 1 byte. And
you’re not freeing this memory nor the rwops. There may be other
issues,
but try allocating the buffer like so:
char buf[MAX_BUF_SIZE];

And then before the compile() function is over:
SDL_RWclose®;

Jonny D

On Saturday, November 7, 2015, brightening-eyes <amir.ramezani1370 at gmail.com> wrote:

hello,
i’m making a game engine and for compiling my scripts, i’m using
SDL_Rwops
the scripting language is squirrel and it works, because it use’s
FILE*,
but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src,
SQInteger

sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Here is some sample code to get you to see how to dynamically allocate data.

SDL_RWops* io = SDL_RWFromFile(file_path, "r");
char* line = NULL;
if (io != NULL) {
    char name[1];
    size_t amnt_read = 0;
    size_t index = 0;
    size_t buffer = 4096; //this is the chunk size
    line = (char*)malloc(sizeof(char) * buffer);
    while (amnt_read = SDL_RWread(io, name, sizeof(name), 1) > 0) {
        line[index] = (char)name[0];
        ++index;
        if (index == buffer - 1) {
            buffer = buffer * 2;
            line = realloc(line, buffer);
            if (line != NULL) {
                SDL_Log("\nreallocing %zu bytes to load %s\n", buffer,
                        file_path);
            } else {
                SDL_Log("\nfailed to realloc %zu bytes to load %s\n",
                        buffer, file_path);
            }
        }
        line = realloc(line, (sizeof(char) * (index + 1)));
        line[index] = '\0';
    }
    line[index] = '\0';

    return line;
    SDL_RWclose(io);
} else {
    SDL_Log(
        "FAILED TO ALLOCATE RESOURCE\n",
        file_path);
}
return line;

the realloc will make the memory grow by 4096 bytes but then in the end it
shrinks back down to fit the memory to what you needed without wasting too
much excess space.

This should give u a good place to start.On Sun, Nov 8, 2015 at 11:31 AM, Jonathan Dearborn wrote:

char* doesn’t grow and the SDL_RWops wouldn’t be able to handle it if you
tried. If you need more explicit help with this, you can email me off-list.

Jonny D

On Sat, Nov 7, 2015 at 10:27 PM, Amir Ramezani < amir.ramezani1370 at gmail.com> wrote:

i’ve specified the maximam size of 256, i think it’s sufficient
but, how to make char* to grow itself up?

???-??-?? ?:?? GMT+??:??, Jonathan Dearborn :

If you can’t specify a maximum size, then you have to count it
dynamically. Either count up the size you need, calculate it somehow,
or
cut it into smaller parts (each with a reasonable max size) that you can
feed into a growable container (e.g. a file or a C++ string object).
Just
remember to delete temporarily allocated memory.

Jonny D

On Sat, Nov 7, 2015 at 9:16 PM, Amir Ramezani < amir.ramezani1370 at gmail.com> wrote:

i don’t know how mutch size should be allocated into memory
because maybe the script get’s longer and the size may not be
sufficient

thanks

???-??-?? ??:?? GMT+??:??, Jonathan Dearborn :

As far as I can tell, you’re not assigning any memory for the RWops
to

work

on. SDL_AllocRW() does not create a working buffer, only the rwops
object. I would instead use SDL_RWFromMem(buf, buf_size) to create
the

rwops.

It also looks like your memory buffer only has a size of 1 byte. And
you’re not freeing this memory nor the rwops. There may be other
issues,
but try allocating the buffer like so:
char buf[MAX_BUF_SIZE];

And then before the compile() function is over:
SDL_RWclose®;

Jonny D

On Saturday, November 7, 2015, brightening-eyes <amir.ramezani1370 at gmail.com> wrote:

hello,
i’m making a game engine and for compiling my scripts, i’m using
SDL_Rwops
the scripting language is squirrel and it works, because it use’s
FILE*,
but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src,
SQInteger

sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

i know realloc, but what about with new and delete?
i want to use C++ memory management ruteens, and i don’t know
something other than new and delete
thanks

???-??-?? ?:?? GMT+??:??, Owen Alanzo Hogarth :> Here is some sample code to get you to see how to dynamically allocate

data.

SDL_RWops* io = SDL_RWFromFile(file_path, "r");
char* line = NULL;
if (io != NULL) {
    char name[1];
    size_t amnt_read = 0;
    size_t index = 0;
    size_t buffer = 4096; //this is the chunk size
    line = (char*)malloc(sizeof(char) * buffer);
    while (amnt_read = SDL_RWread(io, name, sizeof(name), 1) > 0) {
        line[index] = (char)name[0];
        ++index;
        if (index == buffer - 1) {
            buffer = buffer * 2;
            line = realloc(line, buffer);
            if (line != NULL) {
                SDL_Log("\nreallocing %zu bytes to load %s\n", buffer,
                        file_path);
            } else {
                SDL_Log("\nfailed to realloc %zu bytes to load %s\n",
                        buffer, file_path);
            }
        }
        line = realloc(line, (sizeof(char) * (index + 1)));
        line[index] = '\0';
    }
    line[index] = '\0';

    return line;
    SDL_RWclose(io);
} else {
    SDL_Log(
        "FAILED TO ALLOCATE RESOURCE\n",
        file_path);
}
return line;

the realloc will make the memory grow by 4096 bytes but then in the end it
shrinks back down to fit the memory to what you needed without wasting too
much excess space.

This should give u a good place to start.

On Sun, Nov 8, 2015 at 11:31 AM, Jonathan Dearborn wrote:

char* doesn’t grow and the SDL_RWops wouldn’t be able to handle it if you
tried. If you need more explicit help with this, you can email me
off-list.

Jonny D

On Sat, Nov 7, 2015 at 10:27 PM, Amir Ramezani < @Amir_Ramezani> wrote:

i’ve specified the maximam size of 256, i think it’s sufficient
but, how to make char* to grow itself up?

???-??-?? ?:?? GMT+??:??, Jonathan Dearborn :

If you can’t specify a maximum size, then you have to count it
dynamically. Either count up the size you need, calculate it somehow,
or
cut it into smaller parts (each with a reasonable max size) that you
can
feed into a growable container (e.g. a file or a C++ string object).
Just
remember to delete temporarily allocated memory.

Jonny D

On Sat, Nov 7, 2015 at 9:16 PM, Amir Ramezani < @Amir_Ramezani> wrote:

i don’t know how mutch size should be allocated into memory
because maybe the script get’s longer and the size may not be
sufficient

thanks

???-??-?? ??:?? GMT+??:??, Jonathan Dearborn :

As far as I can tell, you’re not assigning any memory for the RWops
to

work

on. SDL_AllocRW() does not create a working buffer, only the rwops
object. I would instead use SDL_RWFromMem(buf, buf_size) to create
the

rwops.

It also looks like your memory buffer only has a size of 1 byte.
And
you’re not freeing this memory nor the rwops. There may be other
issues,
but try allocating the buffer like so:
char buf[MAX_BUF_SIZE];

And then before the compile() function is over:
SDL_RWclose®;

Jonny D

On Saturday, November 7, 2015, brightening-eyes <@Amir_Ramezani> wrote:

hello,
i’m making a game engine and for compiling my scripts, i’m using
SDL_Rwops
the scripting language is squirrel and it works, because it use’s
FILE*,
but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src,
SQInteger

sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

guys, this is the newly modified code, it doesn’t crash, but it give’s
an empty string:
string Compile()
{
char* buf=new char[1024];
SDL_RWops r=SDL_RWFromMem(buf, 1024);
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, (void
) r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, (void*) r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
SDL_RWclose®;
return buf;
}

what to do in order to compile it successfully into string?
thanks

???-??-?? ?:?? GMT+??:??, Amir Ramezani <@Amir_Ramezani>:> i know realloc, but what about with new and delete?

i want to use C++ memory management ruteens, and i don’t know
something other than new and delete
thanks

???-??-?? ?:?? GMT+??:??, Owen Alanzo Hogarth :

Here is some sample code to get you to see how to dynamically allocate
data.

SDL_RWops* io = SDL_RWFromFile(file_path, "r");
char* line = NULL;
if (io != NULL) {
    char name[1];
    size_t amnt_read = 0;
    size_t index = 0;
    size_t buffer = 4096; //this is the chunk size
    line = (char*)malloc(sizeof(char) * buffer);
    while (amnt_read = SDL_RWread(io, name, sizeof(name), 1) > 0) {
        line[index] = (char)name[0];
        ++index;
        if (index == buffer - 1) {
            buffer = buffer * 2;
            line = realloc(line, buffer);
            if (line != NULL) {
                SDL_Log("\nreallocing %zu bytes to load %s\n",

buffer,
file_path);
} else {
SDL_Log("\nfailed to realloc %zu bytes to load %s\n",
buffer, file_path);
}
}
line = realloc(line, (sizeof(char) * (index + 1)));
line[index] = ‘\0’;
}
line[index] = ‘\0’;

    return line;
    SDL_RWclose(io);
} else {
    SDL_Log(
        "FAILED TO ALLOCATE RESOURCE\n",
        file_path);
}
return line;

the realloc will make the memory grow by 4096 bytes but then in the end
it
shrinks back down to fit the memory to what you needed without wasting
too
much excess space.

This should give u a good place to start.

On Sun, Nov 8, 2015 at 11:31 AM, Jonathan Dearborn wrote:

char* doesn’t grow and the SDL_RWops wouldn’t be able to handle it if
you
tried. If you need more explicit help with this, you can email me
off-list.

Jonny D

On Sat, Nov 7, 2015 at 10:27 PM, Amir Ramezani < @Amir_Ramezani> wrote:

i’ve specified the maximam size of 256, i think it’s sufficient
but, how to make char* to grow itself up?

???-??-?? ?:?? GMT+??:??, Jonathan Dearborn :

If you can’t specify a maximum size, then you have to count it
dynamically. Either count up the size you need, calculate it
somehow,
or
cut it into smaller parts (each with a reasonable max size) that you
can
feed into a growable container (e.g. a file or a C++ string object).
Just
remember to delete temporarily allocated memory.

Jonny D

On Sat, Nov 7, 2015 at 9:16 PM, Amir Ramezani < @Amir_Ramezani> wrote:

i don’t know how mutch size should be allocated into memory
because maybe the script get’s longer and the size may not be
sufficient

thanks

???-??-?? ??:?? GMT+??:??, Jonathan Dearborn :

As far as I can tell, you’re not assigning any memory for the
RWops
to

work

on. SDL_AllocRW() does not create a working buffer, only the
rwops
object. I would instead use SDL_RWFromMem(buf, buf_size) to
create
the

rwops.

It also looks like your memory buffer only has a size of 1 byte.
And
you’re not freeing this memory nor the rwops. There may be other
issues,
but try allocating the buffer like so:
char buf[MAX_BUF_SIZE];

And then before the compile() function is over:
SDL_RWclose®;

Jonny D

On Saturday, November 7, 2015, brightening-eyes <@Amir_Ramezani> wrote:

hello,
i’m making a game engine and for compiling my scripts, i’m using
SDL_Rwops
the scripting language is squirrel and it works, because it use’s
FILE*,
but i want to compile my scripts into memory
this is my code:
SQInteger rwops_write(SQUserPointer dest, SQUserPointer src,
SQInteger

sz)
{
SDL_RWops* rw = (SDL_RWops*)dest;
return SDL_RWwrite(rw, src, sz, 1) == 1 ? SQ_OK : SQ_ERROR;
}

string compile()
{
char* buf=new char;
SDL_RWops *r=SDL_AllocRW();
if(!r)
{
Error::Throw(vm, “could not allocate memory”);
return nullptr;
}
#if !defined (SCRAT_NO_ERROR_CHECKING)
if(!sq_isnull(obj))
{
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
}
#else
sq_pushobject(vm, obj);
sq_writeclosure(vm, rwops_write, r);
#endif
sq_pop(vm, 1);
SDL_RWread(r, buf, SDL_RWsize®, 1);
return buf;
}

what is the problem, and how to fix it
thanks, amir


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org