Languages :: Delphi :: DLL and memory allocation .... |
|||
| By: VB guy |
Date: 13/02/2003 00:00:00 |
Points: 75 | Status: Answered Quality : Excellent |
|
Hi, im working on an app that reads an untyped file. i have a fucntion in a dll wich opens the file a loads its content into memory ... it works fine, but when i open 2 big files one after the open, i get an access violation error ... wich is caused by a New function wich initalizes my records ... when i copy that code and put it in my main app, it works fine, but in the dll i gess access violation ... thanks for any help |
|||
| By: VGR | Date: 13/02/2003 08:30:00 | Type : Comment |
|
| could I see your entire code ? (at least the part where you assign, read, close and re-open ) |
|||
| By: TheFalklands | Date: 13/02/2003 13:28:00 | Type : Comment |
|
| and to make sure that the file gets closed even during errors by using try ... finally: AssignFile(MyFile, 'somename.txt'); try Reset(MyFile); // do whatever finally CloseFile(MyFile); end; |
|||
| By: collegeBoy | Date: 13/02/2003 15:34:00 | Type : Comment |
|
| TheFalklands, u really should learn to read .. lol well i could put code, its in a LoadInventory fonction wich can be called several time, the second time i call it on the same file, it gives me the error. ---------------------------------------- function LoadInventory (var Inventory : pInventory ; FileName : ShortString) : Boolean ; stdcall ; var Readf : File ; FileResult : Integer ; NewCls, LastCls : pClass ; NewPro, LastPro : pProduct ; i, j : Integer ; begin { Opens The File } AssignFile(Readf, FileName) ; try {$I-} FileMode := fmOpenRead ; Reset(Readf) ; FileResult := IOResult ; {$I+} if FileResult = 0 then begin { Creates The Inventory } Inventory := NewInventory ; { Reads The Inventory } BlockRead(Readf, Inventory^, 1) ; if not Eof(Readf) then begin LastCls := nil ; for i := 1 to Inventory.ClassCount do begin ... reads multiple records ... end ; end ; LoadInventory := True ; end else begin Inventory := nil ; ErrorFileMissing (FileName) ; LoadInventory := False ; end ; finally CloseFile(Readf) ; end ; end ; |
|||
| By: VGR | Date: 13/02/2003 17:52:00 | Type : Comment |
|
| 1) are you sure that the second call provides the same argument FileName to the function ? (could you do some writeLn tracking?) 2) BIG problem, testing IOResult is usually AFTER {$I+} so that getting the value resets the IOResult value for the ext call (this may be the CAUSE of your problem : ie an undetected error proboking an exception next time) 3) you could try without try..finally because reading from a file is not supposed to generate exceptions, especially on untyped files (à part from the ReSet() error caught by {$I-}/{$I}/IOResult, as usual). 4) Check this first, we'll see if I need to think more than 10 seconds on your code if it fails to solve your problem. regards |
|||
| By: VGR | Date: 13/02/2003 17:54:00 | Type : Comment |
|
| and 5) is the openmode useful? ReSet() is ALWAYS "r", Append is ALWAYS "rw" and ReWrite is ALWAYS "w" |
|||
| By: collegeBoy | Date: 14/02/2003 02:23:00 | Type : Comment |
|
| thank's so i should put FileResult := IOResult after I+ ... altought i dont see why i would need to put I- and I+ then ... and about the try finaly and the openmode, those are attempts at fixing my problems, dont worry i'll remove it if ur solution works. Thanks for the help. |
|||
| By: VGR | Date: 14/02/2003 02:34:00 | Type : Comment |
|
| the classical approach (back in T-P 3.0) is nevertheless to do assign(AssignFile those days ;-) / $I- / reset / $I+ / get IOResult |
|||
| By: collegeBoy | Date: 14/02/2003 02:43:00 | Type : Comment |
|
| ?? what are you talking about, TP 3 ? u mean do assign instead of assignfile ?? |
|||
| By: collegeBoy | Date: 14/02/2003 02:49:00 | Type : Comment |
|
| hum it seams that wasnt the problem ... btw, my fonction reads an inventory, there 1 inventory record, then 1 class record, followed by products belonging to that class, then another class record, followed by its products... it seams that my fonction works fin if i open a file that only contains the inventory record (empty inventories). .. i can open them a multitude of time without problem, but when its a non empty inventory, it gives me a memory acess violation ... i doubt its the problem .. cuz its not a big file .. but ill test my new fucntion for nil values .. i was planning to add this later .. but ill try it if u have any more ideas please tell |
|||
| By: VGR | Date: 14/02/2003 02:55:00 | Type : Answer |
|
| so at least it doesn't come from the multiple ReSet() ;-) beware of the way you handle data :: Inventory := NewInventory ; may very well cause you trouble if ever NewInventory is not properly initialized/allocated |
|||
| By: collegeBoy | Date: 14/02/2003 03:08:00 | Type : Comment |
|
| its sad but ill prob forget about working with a dll ... sadly, but it just give me too much greif and shit ... if anyone can think of something, plz tell me id like to get to the bottom of this at least . thanks. |
|||
| By: VGR | Date: 14/02/2003 03:11:00 | Type : Comment |
|
| isn't the amount of memory available to a DLL fixed ? Are you allocating dynamically from the heap ? I don't want to be gross by evoking segments, but... |
|||
| By: collegeBoy | Date: 14/02/2003 03:58:00 | Type : Comment |
|
| well i have no idea what ur talkign about see ... i guess u can say i am allocating from heap .. altought im not wuite sure what ur talking bout. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








