Your test program doesn't reassign 'val' to the new pointer returned by
realloc(), so in the second printf() you're actually reading from the old
(not the reallocated) memory location.
If you change the function like this it should work as expected:
EMSCRIPTEN_KEEPALIVE void* _realloc(void* ptr, size_t size){
unsigned int* val = ptr;
printf("before %i %i %i %i %i %i \n", val[0], val[1], val[2], val[2],
val[4], val[5]);
void* newPtr = realloc(ptr,size);
val = newPtr;
printf("after %i %i %i %i %i %i \n", val[0], val[1], val[2], val[2],
val[4], val[5]);
return newPtr;
}
Reason's why this code may appear to work on native platforms could be:
- realloc() is allowed to grow the memory "inplace" and return the same
pointer, but this behaviour isn't guaranteed, one should always assume that
a new pointer is returned
- or realloc() might actually return a new pointer, but you're reading from
freed memory, and this only has the same content because nobody else has
overwritten it yet (this sort of bug can be caught with memory debugging
tools like clang address sanitizer, Valgrind or Dr.Memory.
On Monday, 11 November 2019 09:15:29 UTC+1, Gilles Boisson wrote:
> Hello,
>
> I was playing around malloc / realloc to create dynamic buffer accessible
> from JS, and I realized that realloc change my data inside when the
> returned pointer change .
> I'm not a libc expert but i thought realloc doesn't affect data itself.
>
> I did a proxy method for debugging and validate the fact that the realloc
> change data.
>
> EMSCRIPTEN_KEEPALIVE void* _realloc(void* ptr, size_t size){
> unsigned int* val = ptr;
>
> printf("before %i %i %i %i %i %i \n", val[0], val[1], val[2], val[2],
> val[4], val[5]);
> void* newPtr = realloc(ptr,size);
> printf("after %i %i %i %i %i %i \n", val[0], val[1], val[2], val[2],
> val[4], val[5]);
> return newPtr;
> }
>
>
> Here is my makefile
>
> BUILD = emcc
> LINK = emcc
>
> SRC_DIR = src
> BUILD_DIR = dist
> OBJ_DIR = obj
>
> LINK_FLAGS = -O3 -s STANDALONE_WASM -s NO_EXIT_RUNTIME=1 -s
> EXTRA_EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' -s
> EXPORTED_FUNCTIONS='["_malloc","_free"]'
> BUILD_FLAGS = -c -O3
>
> PROJECT_NAME = em_app
>
>
> SOURCES = $(wildcard $(addprefix $(SRC_DIR)/,*.c)) $(wildcard $(addprefix
> $(SRC_DIR)/glmatrix/,*.c))
> OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
>
>
> all: $(PROJECT_NAME)
>
>
>
>
> $(PROJECT_NAME): $(OBJECTS)
> $(LINK) $(OBJECTS) -o $(addprefix $(BUILD_DIR)/,[email protected]) $(LINK_FLAGS)
>
>
> $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
> $(BUILD) $< -o $@ $(BUILD_FLAGS)
>
> clean:
> rm -f $(addprefix $(BUILD_DIR)/,$(PROJECT_NAME).js) $(addprefix
> $(BUILD_DIR)/,$(PROJECT_NAME).wasm) $(OBJECTS)
>
>
> Any chance to have your lights on the subject.
>
> Cheers,
>
>
--
You received this message because you are subscribed to the Google Groups
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/emscripten-discuss/00c33ab1-ff5d-4e17-b90b-7b1ebed29a68%40googlegroups.com.