Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29242 Discussions

Memory Alocation error - Access violation writing using Heap Functions

faltik_marco
Beginner
1,214 Views
I'm creating a program with two threads one is the consumer and the other is the productor (actualy I've three modules one is the mais program , other is the variables module, other is the consumer, and other is the productor). I need tyo create a commom stack for the threads, produtor read from the keyboard numbers and put them on the heap and consumidor reads the last number read and writtes it 100 times. Everything works fine, but when I use heapalloc function it crashes saying ("primeira tentativa" is program name):

First-chance exception at 0x00401134 in primeiratentativa.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x00401134 in primeiratentativa.exe: 0xC0000005: Access violation writing location 0x00000000.
The program '[1512] primeiratentativa.exe: Native' has exited with code -1073741819 (0xc0000005).


I tried different solutions none of them worked. I read something about shutting down Data execution prevention (DEP), is this something i should try? It doesn't sound like a solution to me.
The code:

module produtor
use variaveis





contains
subroutine produtor_s

use ifmt
use ifcore
USE DFWIN

heaphandle = heapcreate(zero,zero,zero)
if (heaphandle==null) then
print *, "erro"
end if
ponteiro = heapalloc(heaphandle,HEAP_NO_SERIALIZE,size4)
ponteiroaux => ponteiro
read (*,*) ultimonumero

ponteiro => ultimonumero

ponteiroaux = heaprealloc(heaphandle,0,ponteiro,size4)
read (*,*) ultimonumero
ponteiroaux => ultimonumero



j=1
do while(j<100)

ponteiroaux = heaprealloc(heaphandle,0,ponteiro,size4)
read (*,*) ultimonumero
ponteiroaux => ultimonumero

j=j+1
end do
lixo = heapdestroy(heaphandle)

end subroutine produtor_s


end module produtor

The other modules used:

module consumidor



contains
subroutine consumidor_s

use ifcore
use ifmt
use dfwin

i=1

do while (i<100)
print *, ponteiroaux
i=i+1
end do



end subroutine consumidor_s


end module consumidor








module variaveis

use ifmt
use ifcore

public

double precision, DIMENSION(:), ALLOCATABLE :: numero
integer i,N,t,j
integer(4) :: threadhandle
integer(4) :: iretlog
integer(4) :: iretint
integer(4) :: threadhandle1
integer(4) :: iretlog1
integer(4) :: iretint1
integer(4) :: WaitResult
integer(4) :: Mutex
integer(4) :: ivalue0 = 12345678
integer(4) :: ivalue1 = 12345778
integer(4), target :: ultimonumero
integer(4) :: thread1
integer(4) :: thread0
integer(4) :: size4 = 4
integer(4) :: heaphandle
integer(4),pointer :: ponteiro
integer(4),pointer :: ponteiroaux
integer(4) :: zero=0
integer(4) :: lixo

end module variaveis






program programa
!Autor: Marco Falcone Tikerpe
use ifcore
use ifmt
use variaveis
use produtor
use consumidor




threadhandle = CreateThread(0,0,produtor_s,loc(ivalue0),CREATE_SUSPENDED,thread0)
iretlog = SetThreadPriority(threadhandle,THREAD_PRIORITY_BELOW_NORMAL)


threadhandle1 = CreateThread(0,0,consumidor_s,loc(ivalue1),CREATE_SUSPENDED,thread1)
iretlog1 = SetThreadPriority(threadhandle1,THREAD_PRIORITY_NORMAL)


iretint = ResumeThread(threadhandle)
!WaitResult = WaitForSingleObject(numero(N),5000)
call sleepqq(5000)

do while(t<60)
Mutex = CreateMutex(0,.true.,ultimonumero)
iretint1 = ResumeThread(threadhandle1)
Mutex = ReleaseMutex(Mutex)

!WaitResult = WaitForSingleObject(numero(N),5000)
t=t+1
call sleepqq(5000)
end do

end program programa

0 Kudos
4 Replies
Vladimir_P_1234567890
1,214 Views
Is this BASIC?
0 Kudos
faltik_marco
Beginner
1,214 Views
no! It's Fortran. thanks
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,214 Views
heapalloc returns the address of the allocation as an integer of type LPVOID (C_PTR).
Therefor your error is

ponteiroaux => ponteiro

points ponteiroaux at the variable holding the address of the buffer and not pointing at the buffer itself which is held inside ponterio. (you are missing one level of indirection)

Try

call C_F_POINTER(ponteiro, ponteiroaux)

Jim Dempsey



0 Kudos
Reply