project-navigation
Personal tools

Author Topic: Question about mem.cpp  (Read 4123 times)

Nokim

  • Guest
Question about mem.cpp
« on: June 10, 2012, 05:23:37 pm »
I see in mem.cpp expression like
Code: [Select]
(uintptr_t)mem % MEM_HASH but i can't see how it can produce unique (at least in pool) value!

After memory allocation we get some pointer. Say it's 56, we save it in slot 1 (56%11=1), updating pool and so on. Next time we get 144 and... save it in slot 1 (144%11=1). Losing already saved pointer and associated memory. And ready to get any bug possible! But game is working fine... Well, mine copy not so fine, why i am looking here.

UPD: Well, i understood. Things are more complicated :) But that doesn't solve my main problem... :(

This is main question... There is other thing:

In _Mem_ReAlloc required size is just checked if it's equal to already allocated. But allocated size in not the same size as was requested for allocation! It is rounded up for 4-byte alignment! So, memory can be reallocated even if there is no need for this.

« Last Edit: June 10, 2012, 05:46:45 pm by Nokim »

Offline bayo

  • Professional loser
  • Project Coder
  • Captain
  • ***
  • Posts: 733
    • View Profile
Re: Question about mem.cpp
« Reply #1 on: June 10, 2012, 05:38:22 pm »
That's a hashmap, eash slot contains list, not unique element. You can check memBlock_t and the "next" element to iterate all elements of the list.

Nokim

  • Guest
Re: Question about mem.cpp
« Reply #2 on: June 10, 2012, 06:16:00 pm »
Thanks. I'm trying to understand why game is crashing every time on exit. As shown here commenting out redeclaration of new and delete operators removes those crashes. Now i think this is related to freeing memory which external libs expect to free by themselves (and using our delete).

UPD: Yes, seems i am right: if i add some var to disable delete after call to Mem_Shutdown, all is OK. This doesn't eliminate reading of freed (and possibly corrupted) memory though. But no those annoying crashes.
« Last Edit: June 10, 2012, 06:46:17 pm by Nokim »

Offline bayo

  • Professional loser
  • Project Coder
  • Captain
  • ***
  • Posts: 733
    • View Profile
Re: Question about mem.cpp
« Reply #3 on: June 16, 2012, 08:39:34 am »
Then you can check which allocation is wrong?

Nokim

  • Guest
Re: Question about mem.cpp
« Reply #4 on: June 16, 2012, 11:45:42 am »
Right now i have commented out new and delete operators and all is working fine. I've tried to investigate remained errors reported by valgrind, but they seems wrongly detected (except memory leaks maybe), at least i can't say what is wrong with code looking at reported place. Many errors of wrong reading type are related to CPU optimization in libc, when code reads strings by 8-bytes (64 bits) blocks, but i believe it's safely processed.

I can say that all that memory allocation related to crash where done in video part (crashes in X11 libs, X.org libs, radeon driver, SDL etc.) Roughly two months ago all was fine, so i guess this is related to some optimization in video drivers (in system or game). There are two possible causes of crash - wrong function used to free memory (our delete in place of original, when memory was allocated by original new) and attempt to free memory already freed in Mem_Shutdown. First seems more real (at least i have read about such thing in TCMALLOC docs and there are complains about memory sentinels which is absent in original new), second can be case too - it's bad idea to free all allocated memory at once without notifying owners (they can try to use it or free). Of course we can be sure that our code is not using freed memory, but we can't say that about external libs of they are using our operator new.

I'm looking for good IDE to work with code. Something better then just editor with syntax highlighting. I have tried Eclipse (don't understand that thing) and Codeblocks (crashes on opening game project). Now looking with Anjuta but it lacks some features.