GlobalAlloc
Hi there, I''m having the oddest problem. I''ve been using GlobalAlloc to allocate memory within my game code (I''m using assebmler) and it''s works great except for in one function! When I call it in one of my function the program just disappears! Odd eh?
All I did was
mov eax, 4 ;This value could be anything
invoke GlobalAlloc, GMEM_FIXED, eax
and it my game disappears? Anybody got any suggestions? I''m totally confused this is all I got in that procedure, If I comment out the "GlobalAlloc" line everything works except I''m not getting my memory.
Thanks,
Ben
P.S. I''m learning to create linked lists and I''m using this to allocate each node. Is there another way? Apparently there''s no difference from the local heap and global heap in Win32, is this true?
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Not sure exactly what''s wrong..
Try stepping into the GlobalAlloc function if you can and see what''s happening. If your app disappears, it''s probably because something is pointing into memory your app doesn''t own; and Windows kills your app without any warnings or dialogs.
Also, you might want to try and create a small project to test your call to GlobalAlloc(); make sure you can get it working correctly in there [better controlled environment] first.
// CHRIS
Try stepping into the GlobalAlloc function if you can and see what''s happening. If your app disappears, it''s probably because something is pointing into memory your app doesn''t own; and Windows kills your app without any warnings or dialogs.
Also, you might want to try and create a small project to test your call to GlobalAlloc(); make sure you can get it working correctly in there [better controlled environment] first.
// CHRIS
// CHRIS [win32mfc]
Yah that makes sense except I''m only trying to allocate it. I haven''t attempted to access it yet. Shouldn''t it return NULL if I can''t have the requested memory amount?
- Ben
- Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Well, that''s sort of what I was getting at. I assumed your program crashed sometime after your call to GlobalAlloc() but before it returned.
Another thought .. make sure you have the GlobalAlloc function imported properly and you''re linking to the right .LIB or .DLL file (if that''s necessary.)
It''s been a while since I used a stand-alone assembler.
Are you using MASM or TASM? ... or something else?
// CHRIS
// CHRIS [win32mfc]
I''m using MASM and it''s all imported fine... MASM will also check the importing and libs and the number of arguments and wheather the arguments are the correct size, before compiling.
I''ve been using GlobalAlloc in other places in my program and it works?? That''s what''s so odd? I''ve got it working in other modules just this function within this module doesn''t! Well I guess I''ll go check the lib''s and inc''s anyways.
Any other idea''s?
- Ben
I''ve been using GlobalAlloc in other places in my program and it works?? That''s what''s so odd? I''ve got it working in other modules just this function within this module doesn''t! Well I guess I''ll go check the lib''s and inc''s anyways.
Any other idea''s?
- Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Hmm.. I don''t know if it applies, but how are your ''code'' segments set up in the other modules where you have called GlobalAlloc() successfully? Could it be a NEAR/FAR problem?
If you can step through the code, or at least see the machine-language generated (that should be an output option on the assembler), do you get the same instructions generated for the GlobalAlloc() as you do in the other parts of your program (where it works)?
Sorry... It''s been a while; I''m not sure what "invoke" is or how it works. Crashing, though, definitely smacks of something not set up right; either in the stack or the call itself.
// CHRIS
If you can step through the code, or at least see the machine-language generated (that should be an output option on the assembler), do you get the same instructions generated for the GlobalAlloc() as you do in the other parts of your program (where it works)?
Sorry... It''s been a while; I''m not sure what "invoke" is or how it works. Crashing, though, definitely smacks of something not set up right; either in the stack or the call itself.
// CHRIS
// CHRIS [win32mfc]
If you don't figure it out, maybe you can post the routine that fails. (And one that doesn't.)
Not a whole lot to go on; I'm trying to think of all the peripheral circumstances that could cause the problem you're seeing. Hope you figure it out tho!
// CHRIS
Edited by - win32mfc on July 11, 2000 2:27:42 AM
Not a whole lot to go on; I'm trying to think of all the peripheral circumstances that could cause the problem you're seeing. Hope you figure it out tho!
// CHRIS
Edited by - win32mfc on July 11, 2000 2:27:42 AM
// CHRIS [win32mfc]
Hmmm... I got it to work, it doesn''t seem to like being with a certain function for some reason? Here maybe I''ll post it so you can see:
This works:
But this doesn''t:
I''m very confused? And as you can see I need it in the if statement. Basically I have an array or entities (8 members of the array) Each entity has an "active" variable, and a "ptr_FirstNode" which is the pointer to the first node in the linked list.. So I''m looping thorugh the 8 entities, the first one I find that''s not active gets activated and then I want to allocate the first node, but for some reason it won''t do it in that if statement??
- Ben
This works:
;########################################################################; BEGIN CreateEntity procedure - Creates an entity;########################################################################CreateEntity proc PlayerType:DWORDinvoke GlobalAlloc, GMEM_FIXED, SIZEOF(sOBJECT) ;Alocate the first nodemov ecx, 1FindOpenEntity: .if Entity[ecx].Active == FALSE mov Entity[ecx].Active, TRUE ;Set the active flag to true .if eax == FALSE ;Check if it allocated ok.. jmp err ;If error jump out .else mov Entity[ecx].NumNodes, 1 ;Set the number of active nodes mov Entity[ecx].ptr_FirstNode, eax ;If we got our node, put in the pointer .endif return ecx ;Return the entity number .endif inc ecx cmp ecx, 9 jb FindOpenEntityerr:;Couldn''t create entityreturn FALSECreateEntity endp;########################################################################; END CreateEntity procedure;########################################################################
But this doesn''t:
;########################################################################; BEGIN CreateEntity procedure - Creates an entity;########################################################################CreateEntity proc PlayerType:DWORDmov ecx, 1FindOpenEntity: .if Entity[ecx].Active == FALSE mov Entity[ecx].Active, TRUE ;Set the active flag to trueinvoke GlobalAlloc, GMEM_FIXED, SIZEOF(sOBJECT) ;Alocate the first node .if eax == FALSE ;Check if it allocated ok.. jmp err ;If error jump out .else mov Entity[ecx].NumNodes, 1 ;Set the number of active nodes mov Entity[ecx].ptr_FirstNode, eax ;If we got our node, put in the pointer .endif return ecx ;Return the entity number .endif inc ecx cmp ecx, 9 jb FindOpenEntityerr:;Couldn''t create entityreturn FALSECreateEntity endp;########################################################################; END CreateEntity procedure;########################################################################
I''m very confused? And as you can see I need it in the if statement. Basically I have an array or entities (8 members of the array) Each entity has an "active" variable, and a "ptr_FirstNode" which is the pointer to the first node in the linked list.. So I''m looping thorugh the 8 entities, the first one I find that''s not active gets activated and then I want to allocate the first node, but for some reason it won''t do it in that if statement??
- Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
And as you can notice, I''ve made sure not to use eax for my loop, and it''s not very complex? Why would it care? Maybe I should change my looping method? By the way my GlobalFree work fine, but I shouldn''t be allocating it outside the if... Well I see what happens. I might change the way I''m looping.
- Ben
- Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement